原文链接: c++11 异步多线程优化fib 使用 future和async
对比国内这些, 写的都是些啥老玩意
多线程是节约时间的, 不跑跑代码咋知道有没有效果????
不过看看自己写的这垃圾, 也就算了吧
https://thispointer.com/c11-multithreading-part-2-joining-and-detaching-threads/
https://solarianprogrammer.com/2012/02/27/cpp-11-thread-tutorial-part-2/
https://thispointer.com/c11-how-to-create-vector-of-thread-objects/
https://thispointer.com/c11-multithreading-part-9-stdasync-tutorial-example/
wasm的支持好像有待提高....
相当的慢, 而且多线程一开直接web主线程无响应....
编译到node下也是问题有点多...
加了参数也无法执行
两个耗时5s的操作串行执行耗时10s
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std::chrono;
std::string fetchDataFromDB(std::string recvdData)
{
// Make sure that function takes 5 seconds to complete
std::this_thread::sleep_for(seconds(5));
//Do stuff like creating DB Connection and fetching Data
return "DB_" + recvdData;
}
std::string fetchDataFromFile(std::string recvdData)
{
// Make sure that function takes 5 seconds to complete
std::this_thread::sleep_for(seconds(5));
//Do stuff like fetching Data File
return "File_" + recvdData;
}
int main()
{
// Get Start Time
system_clock::time_point start = system_clock::now();
//Fetch Data from DB
std::string dbData = fetchDataFromDB("Data");
//Fetch Data from File
std::string fileData = fetchDataFromFile("Data");
// Get End Time
auto end = system_clock::now();
auto diff = duration_cast < std::chrono::seconds > (end - start).count();
std::cout << "Total Time Taken = " << diff << " Seconds" << std::endl;
//Combine The Data
std::string data = dbData + " :: " + fileData;
//Printing the combined Data
std::cout << "Data = " << data << std::endl;
return 0;
}
异步执行, 耗时5s
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <thread>
using namespace std::chrono;
std::string fetchDataFromDB(std::string recvdData) {
// Make sure that function takes 5 seconds to complete
std::this_thread::sleep_for(seconds(5));
// Do stuff like creating DB Connection and fetching Data
return "DB_" + recvdData;
}
std::string fetchDataFromFile(std::string recvdData) {
// Make sure that function takes 5 seconds to complete
std::this_thread::sleep_for(seconds(5));
// Do stuff like fetching Data File
return "File_" + recvdData;
}
int main() {
// Get Start Time
system_clock::time_point start = system_clock::now();
std::future<std::string> resultFromDB =
std::async(std::launch::async, fetchDataFromDB, "Data");
// Fetch Data from File
std::string fileData = fetchDataFromFile("Data");
// Fetch Data from DB
// Will block till data is available in future<std::string> object.
std::string dbData = resultFromDB.get();
// Get End Time
auto end = system_clock::now();
auto diff = duration_cast<std::chrono::seconds>(end - start).count();
std::cout << "Total Time Taken = " << diff << " Seconds" << std::endl;
// Combine The Data
std::string data = dbData + " :: " + fileData;
// Printing the combined Data
std::cout << "Data = " << data << std::endl;
return 0;
}
/*
g++ --std=c++11 -pthread -lpthread fibThread2.cpp
*/
单线程版fib
45个fib,需要20多秒
#include <chrono>
#include <iostream>
#include <thread>
#include <time.h>
#include <vector>
#define LL long long
using namespace std;
int _fib(int n) { return n < 2 ? n : _fib(n - 1) + _fib(n - 2); }
int fib(int n) {
auto st = std::chrono::steady_clock::now();
int f = _fib(n);
auto ed = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(ed - st).count();
cout << "fib time: " << n << "->" << duration << endl;
return f;
}
void single(int size) {
vector<int> v;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < size; i++) {
int f = fib(i);
v.push_back(f);
cout << "fib: " << i << " " << f << endl;
}
auto finish = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)
.count();
cout << "single time: " << duration << endl;
for (int i = 0; i < v.size(); i++)
cout << "fib:" << i << " " << v[i] << endl;
}
int main() {
int size = 45;
single(size);
return 0;
}
/*
single time:
24475548
24305800
20785245
24328843
single time: 2256069
g++ --std=c++11 -lpthread fibThread.cpp
g++ --std=c++11 -pthread -lpthread fibSingle.cpp
*/
所谓的多线程
因为join的原因虽然阻塞了主线程但也是并行, 也是45个fib, 提高到14s左右
其实由于join的原因看着也不太像是多个并行
#include <chrono>
#include <iostream>
#include <pthread.h>
#include <thread>
#include <time.h>
#include <vector>
#define LL long long
using namespace std;
int _fib(int n) { return n < 2 ? n : _fib(n - 1) + _fib(n - 2); }
int fib(int n, int *res) {
clock_t st = clock();
*res = _fib(n);
clock_t ed = clock();
cout << "fib time: " << n << "->" << ed - st << endl;
return *res;
}
void many(int size) {
clock_t start = clock();
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
int list[size];
vector<std::thread> v;
for (int i = 0; i < size; i++) {
std::thread t(fib, i, list + i); // 启动线程foo
// v.push_back(t);
v.push_back(std::move(t));
std::cout << t.hardware_concurrency() << "-->" << t.native_handle()
<< std::endl; //可以并发执行多少个(不准确)
// t.detach(); // 等待线程执行完成
}
for (int i = 0; i < v.size(); i++) {
if (v[i].joinable())
v[i].join(); // 等待线程执行完成
}
clock_t ends = clock();
cout << "many time: " << ends - start << endl;
for (int i = 0; i < size; i++) {
cout << "fib: " << i << " " << *(list + i) << endl;
}
}
int main() {
int size = 45;
many(size);
// std::thread thread(fib); // 启动线程foo
// thread.join(); // 等待线程执行完成
return 0;
}
/*
many time: 2234429
many time: 24892014
many time: 2156435
g++ --std=c++11 -lpthread fibThread.cpp
g++ --std=c++11 -pthread -lpthread fibThread.cpp
*/
异步45个13秒
有一点点并行的味道了
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <thread>
using namespace std::chrono;
int _fib(int n) {
return n < 2 ? n : _fib(n - 1) + _fib(n - 2);
;
}
int fib(int n) {
auto st = std::chrono::steady_clock::now();
int f = _fib(n);
auto ed = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(ed - st).count();
std::cout << "fib time: " << n << " " << duration << std::endl;
return f;
}
int main() {
int size = 45;
auto st = std::chrono::steady_clock::now();
std::future<int> list[size];
for (int i = 0; i < size; i++) {
list[i] = std::async(std::launch::async, fib, i);
}
for (int i = 0; i < size; i++) {
auto res = list[i].get();
std::cout << "res:" << i << "-->" << res << std::endl;
}
auto ed = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(ed - st).count();
std::cout << "future time: " << duration << std::endl;
return 0;
}
/*
g++ --std=c++11 -pthread -lpthread fibThread3.cpp
*/