]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | // Copyright Oliver Kowalke 2009. | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <cstdlib> | |
8 | #include <iostream> | |
9 | #include <stdexcept> | |
10 | #include <thread> | |
11 | ||
12 | #include <boost/cstdint.hpp> | |
13 | #include <boost/preprocessor.hpp> | |
14 | ||
15 | #include "../clock.hpp" | |
16 | ||
17 | #define CREATE(z, n, _) \ | |
18 | std::thread BOOST_PP_CAT(t,n) (worker); | |
19 | #define JOIN(z, n, _) \ | |
20 | BOOST_PP_CAT(t,n) .join(); | |
21 | ||
22 | void worker() {} | |
23 | ||
24 | duration_type measure10( duration_type overhead) | |
25 | { | |
26 | std::thread( worker).join(); | |
27 | ||
28 | BOOST_PP_REPEAT_FROM_TO(1, 10, CREATE, _); | |
29 | ||
30 | time_point_type start( clock_type::now() ); | |
31 | BOOST_PP_REPEAT_FROM_TO(1, 10, JOIN, _); | |
32 | duration_type total = clock_type::now() - start; | |
33 | ||
34 | total -= overhead_clock(); // overhead of measurement | |
35 | total /= 10; // loops | |
36 | ||
37 | return total; | |
38 | } | |
39 | ||
40 | duration_type measure50( duration_type overhead) | |
41 | { | |
42 | std::thread( worker).join(); | |
43 | ||
44 | BOOST_PP_REPEAT_FROM_TO(1, 50, CREATE, _); | |
45 | ||
46 | time_point_type start( clock_type::now() ); | |
47 | BOOST_PP_REPEAT_FROM_TO(1, 50, JOIN, _); | |
48 | duration_type total = clock_type::now() - start; | |
49 | ||
50 | total -= overhead_clock(); // overhead of measurement | |
51 | total /= 50; // loops | |
52 | ||
53 | return total; | |
54 | } | |
55 | ||
56 | duration_type measure100( duration_type overhead) | |
57 | { | |
58 | std::thread( worker).join(); | |
59 | ||
60 | BOOST_PP_REPEAT_FROM_TO(1, 100, CREATE, _); | |
61 | ||
62 | time_point_type start( clock_type::now() ); | |
63 | BOOST_PP_REPEAT_FROM_TO(1, 100, JOIN, _); | |
64 | duration_type total = clock_type::now() - start; | |
65 | ||
66 | total -= overhead_clock(); // overhead of measurement | |
67 | total /= 100; // loops | |
68 | ||
69 | return total; | |
70 | } | |
71 | ||
72 | duration_type measure500( duration_type overhead) | |
73 | { | |
74 | std::thread( worker).join(); | |
75 | ||
76 | #include "thread_create_500.ipp" | |
77 | ||
78 | time_point_type start( clock_type::now() ); | |
79 | #include "thread_join_500.ipp" | |
80 | duration_type total = clock_type::now() - start; | |
81 | ||
82 | total -= overhead_clock(); // overhead of measurement | |
83 | total /= 500; // loops | |
84 | ||
85 | return total; | |
86 | } | |
87 | ||
88 | duration_type measure1000( duration_type overhead) | |
89 | { | |
90 | std::thread( worker).join(); | |
91 | ||
92 | #include "thread_create_1000.ipp" | |
93 | ||
94 | time_point_type start( clock_type::now() ); | |
95 | #include "thread_join_1000.ipp" | |
96 | duration_type total = clock_type::now() - start; | |
97 | ||
98 | total -= overhead_clock(); // overhead of measurement | |
99 | total /= 1000; // loops | |
100 | ||
101 | return total; | |
102 | } | |
103 | ||
104 | duration_type measure5000( duration_type overhead) | |
105 | { | |
106 | std::thread( worker).join(); | |
107 | ||
108 | #include "thread_create_5000.ipp" | |
109 | ||
110 | time_point_type start( clock_type::now() ); | |
111 | #include "thread_join_5000.ipp" | |
112 | duration_type total = clock_type::now() - start; | |
113 | ||
114 | total -= overhead_clock(); // overhead of measurement | |
115 | total /= 5000; // loops | |
116 | ||
117 | return total; | |
118 | } | |
119 | ||
120 | duration_type measure10000( duration_type overhead) | |
121 | { | |
122 | std::thread( worker).join(); | |
123 | ||
124 | #include "thread_create_10000.ipp" | |
125 | ||
126 | time_point_type start( clock_type::now() ); | |
127 | #include "thread_join_10000.ipp" | |
128 | duration_type total = clock_type::now() - start; | |
129 | ||
130 | total -= overhead_clock(); // overhead of measurement | |
131 | total /= 10000; // loops | |
132 | ||
133 | return total; | |
134 | } | |
135 | ||
136 | int main( int argc, char * argv[]) | |
137 | { | |
138 | try | |
139 | { | |
140 | duration_type overhead = overhead_clock(); | |
141 | ||
142 | boost::uint64_t res = measure10( overhead).count(); | |
143 | std::cout << "10 jobs: average of " << res << " nano seconds" << std::endl; | |
144 | res = measure50( overhead).count(); | |
145 | std::cout << "50 jobs: average of " << res << " nano seconds" << std::endl; | |
146 | res = measure100( overhead).count(); | |
147 | std::cout << "100 jobs: average of " << res << " nano seconds" << std::endl; | |
148 | res = measure500( overhead).count(); | |
149 | std::cout << "500 jobs: average of " << res << " nano seconds" << std::endl; | |
150 | res = measure1000( overhead).count(); | |
151 | std::cout << "1000 jobs: average of " << res << " nano seconds" << std::endl; | |
152 | res = measure5000( overhead).count(); | |
153 | std::cout << "5000 jobs: average of " << res << " nano seconds" << std::endl; | |
154 | res = measure10000( overhead).count(); | |
155 | std::cout << "10000 jobs: average of " << res << " nano seconds" << std::endl; | |
156 | ||
157 | return EXIT_SUCCESS; | |
158 | } | |
159 | catch ( std::exception const& e) | |
160 | { std::cerr << "exception: " << e.what() << std::endl; } | |
161 | catch (...) | |
162 | { std::cerr << "unhandled exception" << std::endl; } | |
163 | return EXIT_FAILURE; | |
164 | } |