c++ - Get CPU ticks and measured time -


so want measure time of functions in c++ , have tried many ways of doing so. final code:

auto start = chrono::steady_clock::now(); clstart = clock();  (int o=0; o<100;o++) {     sin(o);    // sleep(1); }  auto end = chrono::steady_clock::now(); clend = clock();  auto diftime = end-start; diff = chrono::duration <double,milli> (diftime).count(); //gives me 0 diffticks = clend-clstart; //0 

it works fine when insert example sleep() function inside when using sin math.h 0 time. why that? know it's optimized 0? want compare other sin implementations not sure wrong here.

the time used calculate 100 sin short, should use

auto start = chrono::steady_clock::now(); clstart = clock();  (int o=0; o<100000;o++) {     sin(o);    // sleep(1); }  auto end = chrono::steady_clock::now(); clend = clock();  auto diftime = end-start; diff = chrono::duration <double,milli> (diftime).count(); diffticks = clend-clstart; 

instead.

furthermore, code measure milliseconds. recommend use chrono::duration <double, nano> (diff).count() use nanosecond more precision.


from doc:

the data stored in duration tick count of type rep. if rep floating point, duration can represent fractions of ticks. period included part of duration's type, , used when converting between different durations.

which means not cpu ticks you're measuring


Comments