Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / context / performance / execution_context_v2 / performance.cpp
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 <cstddef>
8 #include <cstdlib>
9 #include <iostream>
10 #include <stdexcept>
11
12 #include <boost/context/execution_context.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
15
16 #include "../clock.hpp"
17 #include "../cycle.hpp"
18
19 boost::uint64_t jobs = 1000000;
20
21 static boost::context::execution_context< void > foo( boost::context::execution_context< void > && ctx) {
22     while ( true) {
23         ctx = ctx();
24     }
25     return std::move( ctx);
26 }
27
28 duration_type measure_time() {
29     // cache warum-up
30     boost::context::execution_context< void > ctx( foo);
31     ctx = ctx();
32
33     time_point_type start( clock_type::now() );
34     for ( std::size_t i = 0; i < jobs; ++i) {
35         ctx = ctx();
36     }
37     duration_type total = clock_type::now() - start;
38     total -= overhead_clock(); // overhead of measurement
39     total /= jobs;  // loops
40     total /= 2;  // 2x jump_fcontext
41
42     return total;
43 }
44
45 #ifdef BOOST_CONTEXT_CYCLE
46 cycle_type measure_cycles() {
47     // cache warum-up
48     boost::context::fixedsize_stack alloc;
49     boost::context::execution_context< void > ctx( std::allocator_arg, alloc, foo);
50     ctx = ctx();
51
52     cycle_type start( cycles() );
53     for ( std::size_t i = 0; i < jobs; ++i) {
54         ctx = ctx();
55     }
56     cycle_type total = cycles() - start;
57     total -= overhead_cycle(); // overhead of measurement
58     total /= jobs;  // loops
59     total /= 2;  // 2x jump_fcontext
60
61     return total;
62 }
63 #endif
64
65 int main( int argc, char * argv[]) {
66     try {
67         boost::program_options::options_description desc("allowed options");
68         desc.add_options()
69             ("help", "help message")
70             ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
71
72         boost::program_options::variables_map vm;
73         boost::program_options::store(
74                 boost::program_options::parse_command_line(
75                     argc,
76                     argv,
77                     desc),
78                 vm);
79         boost::program_options::notify( vm);
80
81         if ( vm.count("help") ) {
82             std::cout << desc << std::endl;
83             return EXIT_SUCCESS;
84         }
85
86         boost::uint64_t res = measure_time().count();
87         std::cout << "execution_context: average of " << res << " nano seconds" << std::endl;
88 #ifdef BOOST_CONTEXT_CYCLE
89         res = measure_cycles();
90         std::cout << "execution_context: average of " << res << " cpu cycles" << std::endl;
91 #endif
92
93         return EXIT_SUCCESS;
94     } catch ( std::exception const& e) {
95         std::cerr << "exception: " << e.what() << std::endl;
96     } catch (...) {
97         std::cerr << "unhandled exception" << std::endl;
98     }
99     return EXIT_FAILURE;
100 }