time vs gettimeofday c ++
Asked Answered
B

4

6

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.

Bebel answered 1/3, 2013 at 13:24 Comment(3)
Unless you have a long running task, time() is a terrible idea. It only counts seconds, whilist gettimeofday() can return microseconds.Clockmaker
Could you clarify whether time refers to the C library function, or the shell command? And could you give examples of the difference you're seeing?Monoacid
I was actually referring to the shell command time!Bebel
M
6

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... ;)

Micro answered 1/3, 2013 at 13:30 Comment(1)
Thanks! I was hoping to find a subtle difference!Bebel
C
6

It's all dependent on what you are timing. If you are trying to time something in seconds, then time() is probably your best bet. If you need higher resolution than that, then I would consider gettimeofday(), which gives up to microsecond resolution (1 / 1000000th of a second).

If you need even higher resolution than that, consider using clock() and CLOCKS_PER_SECOND, just note that clock() is rarely an accurate description for the amount of time taken, but rather the number of CPU cycles used.

Clockmaker answered 1/3, 2013 at 13:29 Comment(0)
M
6

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... ;)

Micro answered 1/3, 2013 at 13:30 Comment(1)
Thanks! I was hoping to find a subtle difference!Bebel
H
5

time() returns time since epoch in seconds.

gettimeofday(): returns:

struct timeval {
       time_t      tv_sec;     /* seconds */
       suseconds_t tv_usec;    /* microseconds */
};
Harim answered 1/3, 2013 at 13:30 Comment(0)
D
1

Each time function has different precision. In C++11 you would use std::chrono:

using namespace std::chrono;

auto start = high_resolution_clock::now();
/* do stuff*/
auto end = high_resolution_clock::now();

float elapsedSeconds = duration_cast<duration<float>>(end-start).count();
Doretha answered 1/3, 2013 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.