1 // Copyright 2011 Vicente J. Botet Escriba 2 // Copyright (c) Microsoft Corporation 2014 3 4 // Distributed under the Boost Software License, Version 1.0. 5 // See http://www.boost.org/LICENSE_1_0.txt 6 7 #include <boost/chrono/chrono.hpp> 8 #include <boost/chrono/chrono_io.hpp> 9 #include <libs/chrono/example/timer.hpp> 10 #include <boost/chrono/process_cpu_clocks.hpp> 11 #include <vector> 12 13 //#define BOOST_CHRONO_HAS_TIMES_AND_CLOCK 14 15 #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK 16 #include <sys/time.h> //for gettimeofday and timeval 17 #include <sys/times.h> //for times 18 #include <unistd.h> 19 #endif 20 21 22 static const std::size_t size = 1000000; 23 24 typedef boost::chrono::timer<boost::chrono::high_resolution_clock> Stopwatch; 25 26 template <typename Clock> perf_constant(std::vector<typename Clock::time_point> & vec)27void perf_constant(std::vector<typename Clock::time_point>& vec) 28 { 29 for (int i=size-1; i>=0; --i) 30 { 31 vec[i]=typename Clock::time_point(); 32 } 33 } 34 35 template <typename Clock> perf(std::vector<typename Clock::time_point> & vec)36void perf(std::vector<typename Clock::time_point>& vec) 37 { 38 for (int i=size-1; i>=0; --i) 39 { 40 vec[i]=Clock::now(); 41 } 42 } 43 44 template <typename Clock> test()45void test() 46 { 47 std::vector<typename Clock::time_point> vec(size); 48 Stopwatch sw1; 49 perf_constant<Clock>(vec); 50 Stopwatch::duration t1 = sw1.elapsed(); 51 Stopwatch sw2; 52 perf<Clock>(vec); 53 Stopwatch::duration t2 = sw2.elapsed(); 54 std::cout <<" "<< (t2-t1) << std::endl; 55 //std::cout <<" "<< ((t2-t1)/size) << std::endl; 56 std::size_t cnt=0; 57 for (int i=size-1; i>0; --i) 58 { 59 if (vec[i]!=vec[i-1]) ++cnt; 60 } 61 std::cout <<"changes: "<< cnt << std::endl; 62 63 } 64 65 66 67 68 69 #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK perf2(std::vector<clock_t> & vec)70void perf2(std::vector<clock_t>& vec) 71 { 72 Stopwatch sw; 73 for (int i=size-1; i>=0; --i) 74 { 75 tms tm; 76 vec[i]=::times(&tm); 77 } 78 std::cout << sw.elapsed() << std::endl; 79 } 80 perf3(std::vector<clock_t> & vec)81void perf3(std::vector<clock_t>& vec) 82 { 83 Stopwatch sw; 84 for (int i=size-1; i>=0; --i) 85 { 86 vec[i]=::clock(); 87 } 88 std::cout << sw.elapsed() << std::endl; 89 } 90 test2()91void test2() 92 { 93 std::vector<clock_t> vec(size); 94 perf2(vec); 95 std::size_t cnt=0; 96 for (int i=10; i>0; --i) 97 { 98 if (vec[i]!=vec[i-1]) ++cnt; 99 std::cout << vec[i] << " " ; 100 } 101 std::cout<< std::endl; 102 std::cout <<"changes: "<< cnt << std::endl; 103 } 104 test3()105void test3() 106 { 107 std::vector<clock_t> vec(size); 108 perf3(vec); 109 std::size_t cnt=0; 110 for (int i=10; i>0; --i) 111 { 112 if (vec[i]!=vec[i-1]) ++cnt; 113 std::cout << vec[i] << " " ; 114 } 115 std::cout<< std::endl; 116 std::cout <<"changes: "<< cnt << std::endl; 117 } 118 119 #endif 120 main()121int main() { 122 123 std::cout << "system_clock "; 124 test<boost::chrono::system_clock>(); 125 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY 126 std::cout << "steady_clock " ; 127 test<boost::chrono::steady_clock>(); 128 #endif 129 std::cout << "high_resolution_clock " ; 130 test<boost::chrono::high_resolution_clock>(); 131 132 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) 133 std::cout << "process_real_cpu_clock "; 134 test<boost::chrono::process_real_cpu_clock>(); 135 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP 136 std::cout << "process_user_cpu_clock "; 137 test<boost::chrono::process_user_cpu_clock>(); 138 std::cout << "process_system_cpu_clock " ; 139 test<boost::chrono::process_system_cpu_clock>(); 140 std::cout << "process_cpu_clock " ; 141 test<boost::chrono::process_cpu_clock>(); 142 #endif 143 #endif 144 std::cout << "system_clock "; 145 test<boost::chrono::system_clock>(); 146 #if 0 147 std::cout << "times "; 148 test2(); 149 std::cout << "clock "; 150 test3(); 151 #endif 152 return 1; 153 } 154