Easily measure elapsed time
Posted
by hap497
on Stack Overflow
See other posts from Stack Overflow
or by hap497
Published on 2010-05-11T06:04:29Z
Indexed on
2010/05/11
7:24 UTC
Read the original article
Hit count: 228
I am trying to use time() to measure various points of my program.
What I don't understand is why the values in the before and after are the same? I understand this is not the best way to profile my program, I just want to see how long something take.
printf("**MyProgram::before time= %ld\n", time(NULL));
doSomthing();
doSomthingLong();
printf("**MyProgram::after time= %ld\n", time(NULL));
I have tried:
struct timeval diff, startTV, endTV;
gettimeofday(&startTV, NULL);
doSomething();
doSomethingLong();
gettimeofday(&endTV, NULL);
timersub(&endTV, &startTV, &diff);
printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
How do I read a result of **time taken = 0 26339
? Does that mean 26,339 nanoseconds = 26.3 msec?
What about **time taken = 4 45025
, does that mean 4 seconds and 25 msec?
© Stack Overflow or respective owner