Comparing performance of a task queued to an existing thread vs. new thread for each task.
修订版 | 409f9b966f3094467094eb3685daee1018a3adbd (tree) |
---|---|
时间 | 2017-06-03 09:47:40 |
作者 | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Add in system for selecting subset of tests.
@@ -4,6 +4,7 @@ | ||
4 | 4 | #include <iostream> |
5 | 5 | #include "readerwriterqueue.h" |
6 | 6 | #include <stdexcept> |
7 | +#include <string> | |
7 | 8 | |
8 | 9 | extern void do_something(); |
9 | 10 |
@@ -50,35 +51,47 @@ | ||
50 | 51 | } |
51 | 52 | } |
52 | 53 | |
53 | -int main() | |
54 | +int main(int argc, const char * const argv[]) | |
54 | 55 | { |
55 | 56 | using ::std::cout; |
56 | 57 | using ::std::async; |
57 | 58 | using ::std::thread; |
58 | 59 | using ::std::launch; |
59 | - cout << " Do nothing calls per second: " | |
60 | - << calls_per_second([]() { }, 5) << '\n'; | |
61 | - cout << " Empty calls per second: " | |
62 | - << calls_per_second([]() { do_something(); }, 5) << '\n'; | |
63 | - cout << " New thread calls per second: " | |
64 | - << calls_per_second( | |
60 | + int whichtests = 31; // All of them, a bitmask. | |
61 | + if (argc > 1) { | |
62 | + whichtests = ::std::stoi(argv[1]); | |
63 | + } | |
64 | + if (whichtests & 0x1) { | |
65 | + cout << " Do nothing calls per second: " | |
66 | + << calls_per_second([]() { }, 5) << '\n'; | |
67 | + } | |
68 | + if (whichtests & 0x2) { | |
69 | + cout << " Empty calls per second: " | |
70 | + << calls_per_second([]() { do_something(); }, 5) << '\n'; | |
71 | + } | |
72 | + if (whichtests & 0x4) { | |
73 | + cout << " New thread calls per second: " | |
74 | + << calls_per_second( | |
65 | 75 | []() { |
66 | 76 | thread t{ do_something }; |
67 | 77 | t.join(); |
68 | 78 | }, |
69 | 79 | 5 |
70 | - ) | |
71 | - << '\n'; | |
72 | - cout << " Async launch calls per second: " | |
73 | - << calls_per_second( | |
80 | + ) | |
81 | + << '\n'; | |
82 | + } | |
83 | + if (whichtests & 0x8) { | |
84 | + cout << " Async launch calls per second: " | |
85 | + << calls_per_second( | |
74 | 86 | []() { |
75 | 87 | auto fut = async(launch::async | launch::deferred, do_something); |
76 | 88 | fut.wait(); |
77 | 89 | }, |
78 | 90 | 5 |
79 | - ) | |
80 | - << '\n'; | |
81 | - { | |
91 | + ) | |
92 | + << '\n'; | |
93 | + } | |
94 | + if (whichtests & 0x10) { | |
82 | 95 | ::moodycamel::BlockingReaderWriterQueue<the_call_t> from_main; |
83 | 96 | ::moodycamel::BlockingReaderWriterQueue<bool> to_main; |
84 | 97 | thread worker{ [&from_main, &to_main]() { worker_thread(from_main, to_main); } }; |