-3

How to implement hardware timer on OS Windows. I need to measure computations in milliseconds and nanoseconds.

for (i=0;i<1024;i++){
  //start timer
  //computations
  //end timer
  //printf result(end - start)
}
misolo89
  • 123
  • 1
  • 2
  • 13
  • Use getrusage() see my answer https://stackoverflow.com/questions/52869470/set-time-t-to-miliseconds/52874744#52874744 – manoliar Oct 22 '18 at 13:05
  • I don't think you will be able to measure exact nanosecond values on a Windows machine. – A.R.C. Oct 22 '18 at 13:07
  • A general-purpose desktop OS like Windows is inappropriate for such measurements. To start with, you need a free hardware timer peripheral and a suitable driver for it. That alone will screw up nanosecond accuracy, as will higher-priority interrupts. – Martin James Oct 22 '18 at 13:10
  • 1
    In addition to Windows being desktop fluff, you will need very specialized hardware to get nanosecond accuracy. So running off to get a generic MCU evaluation board won't do either. – Lundin Oct 22 '18 at 13:11
  • He said Windows. I was absent-minded.! – manoliar Oct 22 '18 at 13:20
  • get the API for mouse movement. There should be a millisecond wait and reply – DeerSpotter Oct 22 '18 at 13:23

1 Answers1

1

Try using QueryPerformanceFrequency and QueryPerformanceCounter methods to obtain time in sub millisecond resolution:

LARGE_INTEGER cpu_khz, start_time, end_time;  
QueryPerformanceFrequency(&cpu_khz);
for (i=0;i<1024;i++) {
  QueryPerformanceCounter(&start_time); 
  // computations
  QueryPerformanceCounter(&end_time); 
  double delta_time = (start_time.QuadPart-end_time.QuadPart) / (double)cpu_khz.QuadPart;
  printf("took %.3f seconds", delta_time);
}
gabonator
  • 391
  • 1
  • 9