forked from abduld/cpp-taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
86 lines (64 loc) · 2.49 KB
/
threadpool.cpp
File metadata and controls
86 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 2018/8/31 contributed by Guannan
//
// Examples to test different threadpool implementations:
// - SimpleThreadpool
// - ProactiveThreadpool
#include <taskflow/threadpool/threadpool.hpp>
#include <chrono>
#include <random>
// Procedure: benchmark_empty_jobs
void benchmark_empty_jobs() {
std::cout << "Benchmarking threadpool throughput on empty jobs ...\n";
unsigned thread_num = 4;
unsigned int task_num = 10000000;
auto start = std::chrono::high_resolution_clock::now();
tf::ProactiveThreadpool proactive(thread_num);
for(size_t i=0; i<task_num; i++){
proactive.silent_async([](){});
}
proactive.shutdown();
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << " ms\n";
start = std::chrono::high_resolution_clock::now();
tf::SimpleThreadpool simple(thread_num);
for(size_t i=0; i<task_num; i++){
simple.silent_async([](){});
}
simple.shutdown();
end = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "SimpleThreadpool elapsed time: " << elapsed.count() << " ms\n";
}
// Procedure: benchmark_atomic_add
void benchmark_atomic_add() {
std::cout << "Benchmarking threadpool throughput on atomic add ...\n";
unsigned thread_num = 4;
unsigned int task_num = 10000000;
std::atomic<int> counter(0);
auto start = std::chrono::high_resolution_clock::now();
tf::ProactiveThreadpool proactive(thread_num);
for(size_t i=0; i<task_num; i++){
proactive.silent_async([&counter](){ counter++; });
}
proactive.shutdown();
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << " ms\n";
counter = 0;
start = std::chrono::high_resolution_clock::now();
tf::SimpleThreadpool simple(thread_num);
for(size_t i=0; i<task_num; i++){
simple.silent_async([&counter](){ counter++; });
}
simple.shutdown();
end = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "SimpleThreadpool elapsed time: " << elapsed.count() << " ms\n";
}
// Function: main
int main(int argc, char* argv[]) {
benchmark_empty_jobs();
benchmark_atomic_add();
return 0;
}