Comparing performance of a task queued to an existing thread vs. new thread for each task.
Revision | be5c4a1b3527801f637c4b98fee6cb8ed5c6da4e (tree) |
---|---|
Zeit | 2017-09-01 03:07:52 |
Autor | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Make magnitude of numbers more easily compared by setting a width.
@@ -5,6 +5,7 @@ | ||
5 | 5 | #include "readerwriterqueue.h" |
6 | 6 | #include <stdexcept> |
7 | 7 | #include <string> |
8 | +#include <iomanip> | |
8 | 9 | |
9 | 10 | extern void do_something(); |
10 | 11 |
@@ -57,21 +58,22 @@ | ||
57 | 58 | using ::std::async; |
58 | 59 | using ::std::thread; |
59 | 60 | using ::std::launch; |
61 | + using ::std::setw; | |
60 | 62 | int whichtests = 31; // All of them, a bitmask. |
61 | 63 | if (argc > 1) { |
62 | 64 | whichtests = ::std::stoi(argv[1]); |
63 | 65 | } |
64 | 66 | if (whichtests & 0x1) { |
65 | 67 | cout << " Do nothing calls per second: " |
66 | - << calls_per_second([]() { }, 5) << '\n'; | |
68 | + << setw(10) << calls_per_second([]() { }, 5) << '\n'; | |
67 | 69 | } |
68 | 70 | if (whichtests & 0x2) { |
69 | 71 | cout << " Empty calls per second: " |
70 | - << calls_per_second([]() { do_something(); }, 5) << '\n'; | |
72 | + << setw(10) << calls_per_second([]() { do_something(); }, 5) << '\n'; | |
71 | 73 | } |
72 | 74 | if (whichtests & 0x4) { |
73 | 75 | cout << " New thread calls per second: " |
74 | - << calls_per_second( | |
76 | + << setw(10) << calls_per_second( | |
75 | 77 | []() { |
76 | 78 | thread t{ do_something }; |
77 | 79 | t.join(); |
@@ -82,7 +84,7 @@ | ||
82 | 84 | } |
83 | 85 | if (whichtests & 0x8) { |
84 | 86 | cout << " Async launch calls per second: " |
85 | - << calls_per_second( | |
87 | + << setw(10) << calls_per_second( | |
86 | 88 | []() { |
87 | 89 | auto fut = async(launch::async | launch::deferred, do_something); |
88 | 90 | fut.wait(); |
@@ -96,7 +98,7 @@ | ||
96 | 98 | ::moodycamel::BlockingReaderWriterQueue<bool> to_main; |
97 | 99 | thread worker{ [&from_main, &to_main]() { worker_thread(from_main, to_main); } }; |
98 | 100 | cout << "Worker thread calls per second: " |
99 | - << calls_per_second( | |
101 | + << setw(10) << calls_per_second( | |
100 | 102 | [&from_main, &to_main]() { |
101 | 103 | if (!from_main.enqueue(do_something)) { |
102 | 104 | throw ::std::runtime_error("Unable to send request to worker."); |