forked from progschj/ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
89 lines (68 loc) · 1.78 KB
/
Copy pathexample.cpp
File metadata and controls
89 lines (68 loc) · 1.78 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
87
88
89
#include "MashPool.h"
#include <iostream>
#include <chrono>
uint64_t getTime()
{
namespace sc = std::chrono;
return sc::duration_cast<sc::milliseconds>(sc::system_clock::now().time_since_epoch()).count();
}
// these examples demonstrate two different ways MashPools might be used,
// while also measuring some of their overhead in a not-so-thorough benchmark.
int main()
{
{
std::cout << "running MashPool future..." << std::endl;
uint64_t before = getTime();
MashPool pool{ std::thread::hardware_concurrency() };
std::vector<std::future<int>> results;
for (int i = 0; i < 1000000; ++i)
{
results.emplace_back(
pool.addTaskFuture([i]
{
return i * i;
}
)
);
}
uint64_t sum = 0;
for (auto&& result : results)
{
sum += result.get();
}
uint64_t after = getTime();
std::cout << "MashPool future time: " << after - before << std::endl;
std::cout << "MashPool future sum: " << sum << std::endl;
}
{
std::cout << "running MashPool..." << std::endl;
uint64_t before = getTime();
MashPool pool{ std::thread::hardware_concurrency() };
std::atomic_uint64_t sum = 0;
for (int i = 0; i < 1000000; ++i)
{
pool.addTask([&sum, i]
{
sum += i * i;
}
);
}
uint64_t after = getTime();
std::cout << "MashPool time: " << after - before << std::endl;
std::cout << "MashPool sum: " << sum << std::endl;
}
{
std::cout << "running control..." << std::endl;
uint64_t before = getTime();
uint64_t sum = 0;
// this will just get optimized away in most cases,
// but it's good for checking the sum.
for (int i = 0; i < 1000000; ++i)
{
sum += i * i;
}
uint64_t after = getTime();
std::cout << "control time: " << after - before << std::endl;
std::cout << "control sum: " << sum << std::endl;
}
}