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)
12 #include <boost/context/execution_context.hpp>
13 #include <boost/cstdint.hpp>
14 #include <boost/program_options.hpp>
16 #include "../clock.hpp"
17 #include "../cycle.hpp"
19 boost::uint64_t jobs = 1000000;
21 static boost::context::execution_context< void > foo( boost::context::execution_context< void > && ctx) {
25 return std::move( ctx);
28 duration_type measure_time() {
30 boost::context::execution_context< void > ctx( foo);
33 time_point_type start( clock_type::now() );
34 for ( std::size_t i = 0; i < jobs; ++i) {
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
45 #ifdef BOOST_CONTEXT_CYCLE
46 cycle_type measure_cycles() {
48 boost::context::fixedsize_stack alloc;
49 boost::context::execution_context< void > ctx( std::allocator_arg, alloc, foo);
52 cycle_type start( cycles() );
53 for ( std::size_t i = 0; i < jobs; ++i) {
56 cycle_type total = cycles() - start;
57 total -= overhead_cycle(); // overhead of measurement
58 total /= jobs; // loops
59 total /= 2; // 2x jump_fcontext
65 int main( int argc, char * argv[]) {
67 boost::program_options::options_description desc("allowed options");
69 ("help", "help message")
70 ("jobs,j", boost::program_options::value< boost::uint64_t >( & jobs), "jobs to run");
72 boost::program_options::variables_map vm;
73 boost::program_options::store(
74 boost::program_options::parse_command_line(
79 boost::program_options::notify( vm);
81 if ( vm.count("help") ) {
82 std::cout << desc << std::endl;
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;
94 } catch ( std::exception const& e) {
95 std::cerr << "exception: " << e.what() << std::endl;
97 std::cerr << "unhandled exception" << std::endl;