To test out the async
function in C++
I created the following code:
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>
#include <chrono>
using namespace std;
constexpr size_t numTasks = 32;
mutex ioMtx; // mutex for std::cout access
int getTen() {
this_thread::sleep_for(std::chrono::seconds(1));
return 10;
}
int getTenAndPrintThreadId() {
{
lock_guard<mutex> guard(ioMtx);
cout << this_thread::get_id() << endl;
}
this_thread::sleep_for(std::chrono::seconds(1));
return 10;
}
void runThreads(int (*getIntFn)(), string name) {
int result = 0;
auto start = chrono::high_resolution_clock::now();
vector<future<int>> futures;
futures.reserve(numTasks);
for (int i = 0; i < numTasks; ++i) {
futures.push_back(
async(getIntFn)
);
}
for (int i = 0; i < numTasks; ++i) {
result += futures[i].get();
}
auto end = chrono::high_resolution_clock::now();
auto spanMs = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "==== Results for: " << name << " ====" << endl;
cout << "Calculated result = " << result << endl;
cout << "Total time = " << spanMs.count() << "ms" << endl;
cout << "==== =========================== ====" << endl << endl;
}
int main() {
cout << "Hardware concurrency = " << thread::hardware_concurrency() << endl << endl;
// First test
runThreads(getTen, "GET TEN");
// Second test
runThreads(getTenAndPrintThreadId, "GET TEN AND PRINT THREAD ID");
cin.get();
}
It runs two tests. In each test it spawns 32 tasks via async
. Each task returns an int
. The results of such tasks are then summed together.
In the first test each task just returns 10 after 1 second of delay. In the second test tasks are the same, they just also print thread-id to cout
.
And here is the output of the program, running on my 8 core machine.
Hardware concurrency = 8
==== Results for: GET TEN ====
Calculated result = 320
Total time = 3154ms
==== =========================== ====
10060
16920
520
356
11656
7504
14696
9344
4896
13196
11120
4792
7640
12764
6972
13244
4584
5420
14024
3744
8748
15440
12976
10060
16920
520
14820
356
7504
11656
4888
14696
==== Results for: GET TEN AND PRINT THREAD ID ====
Calculated result = 320
Total time = 2030ms
==== =========================== ====
It is surprising to me that the second test runs faster than the first one, despite having to do more work. If I change numTasks
to even higher number, it is obvious that the second test is generally faster. Why is that? Is there something I misunderstood?
Aucun commentaire:
Enregistrer un commentaire