// Copyright 2011 Vicente J. Botet Escriba // Copyright (c) Microsoft Corporation 2014 // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt #include #include #include #include #include //#define BOOST_CHRONO_HAS_TIMES_AND_CLOCK #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK #include //for gettimeofday and timeval #include //for times #include #endif static const std::size_t size = 1000000; typedef boost::chrono::timer Stopwatch; template void perf_constant(std::vector& vec) { for (int i=size-1; i>=0; --i) { vec[i]=typename Clock::time_point(); } } template void perf(std::vector& vec) { for (int i=size-1; i>=0; --i) { vec[i]=Clock::now(); } } template void test() { std::vector vec(size); Stopwatch sw1; perf_constant(vec); Stopwatch::duration t1 = sw1.elapsed(); Stopwatch sw2; perf(vec); Stopwatch::duration t2 = sw2.elapsed(); std::cout <<" "<< (t2-t1) << std::endl; //std::cout <<" "<< ((t2-t1)/size) << std::endl; std::size_t cnt=0; for (int i=size-1; i>0; --i) { if (vec[i]!=vec[i-1]) ++cnt; } std::cout <<"changes: "<< cnt << std::endl; } #ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK void perf2(std::vector& vec) { Stopwatch sw; for (int i=size-1; i>=0; --i) { tms tm; vec[i]=::times(&tm); } std::cout << sw.elapsed() << std::endl; } void perf3(std::vector& vec) { Stopwatch sw; for (int i=size-1; i>=0; --i) { vec[i]=::clock(); } std::cout << sw.elapsed() << std::endl; } void test2() { std::vector vec(size); perf2(vec); std::size_t cnt=0; for (int i=10; i>0; --i) { if (vec[i]!=vec[i-1]) ++cnt; std::cout << vec[i] << " " ; } std::cout<< std::endl; std::cout <<"changes: "<< cnt << std::endl; } void test3() { std::vector vec(size); perf3(vec); std::size_t cnt=0; for (int i=10; i>0; --i) { if (vec[i]!=vec[i-1]) ++cnt; std::cout << vec[i] << " " ; } std::cout<< std::endl; std::cout <<"changes: "<< cnt << std::endl; } #endif int main() { std::cout << "system_clock "; test(); #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY std::cout << "steady_clock " ; test(); #endif std::cout << "high_resolution_clock " ; test(); #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) std::cout << "process_real_cpu_clock "; test(); #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP std::cout << "process_user_cpu_clock "; test(); std::cout << "process_system_cpu_clock " ; test(); std::cout << "process_cpu_clock " ; test(); #endif #endif std::cout << "system_clock "; test(); #if 0 std::cout << "times "; test2(); std::cout << "clock "; test3(); #endif return 1; }