I am working on encryption of realtime data. I have developed encryption and decryption algorithm. Now i want to measure the execution time of the same on Linux platform in C. How can i correctly measure it ?. I have tried it as below
gettimeofday(&tv1, NULL);
/* Algorithm Implementation Code*/
gettimeofday(&tv2, NULL);
Total_Runtime=(tv2.tv_usec - tv1.tv_usec) +
(tv2.tv_sec - tv1.tv_sec)*1000000);
which gives me time in microseconds. Is it correct way of time measurement or i should use some other function? Any hint will be appreciated.
Read time(7). You probably want to use clock_gettime(2) with
CLOCK_PROCESS_CPUTIME_IDorCLOCK_MONOTONIC. Or you could just use clock(3) (for the CPU time in microseconds, sinceCLOCK_PER_SECis always a million).If you want to benchmark an entire program (executable), use time(1) command.