Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time vs gettimeofday c ++

The time command returns the time elapsed in execution of a command.

If I put a "gettimeofday()" at the start of the command call (using system() ), and one at the end of the call, and take a difference, it doesn't come out the same. (its not a very small difference either)

Can anybody explain what is the exact difference between the two usages and which is the best way to time the execution of a call?

Thanks.

like image 944
sushant-hiray Avatar asked Mar 01 '13 13:03

sushant-hiray


1 Answers

The Unix time command measures the whole program execution time, including the time it takes for the system to load your binary and all its libraries, and the time it takes to clean up everything once your program is finished.

On the other hand, gettimeofday can only work inside your program, that is after it has finished loading (for the initial measurement), and before it is cleaned up (for the final measurement).

Which one is best? Depends on what you want to measure... ;)

like image 65
syam Avatar answered Nov 15 '22 04:11

syam