What does time(NULL)
return? Given documentation I had assumed it returns the number of seconds since epoch, however, when I run the following code:
#include <iostream>
#include "Time.h"
#include "Math.h"
int main () {
double curT = 0;
double curJ = 0;
time_t curTtimeT = 0;
double curJtimeT = 0;
//run indefinitely... use a debugger you can stop :-)
for (double j = 0; j < 2000000000; j++) {
//get the current time
curT = time(NULL);
curTtimeT = time(NULL);
//check if current time equals previous time
if (time(NULL) != curT) {
//output time and approx. "time" through iteration
std::cout << "Time match with doubles:\t" << curT << "\tdeltaJ:\t\t\t" << (j - curJ) <<std::endl;
curT = time(NULL);
curJ = j;
}
if (time(NULL) != curTtimeT) {
//output time and approx. "time" through iteration
std::cout << "Time match with time_t:\t\t" << curTtimeT << "\t\tdeltaJtimeT:\t\t" << (j - curJtimeT) <<std::endl;
curTtimeT = time(NULL);
curJtimeT = j;
}
}
return 0;
}
I get the following results (the skipping does not appear to have a pattern between runs):
Time match with time_t: 1348873002 deltaJtimeT: 290842
Time match with doubles: 1.34887e+09 deltaJ: 2.41017e+06
Time match with doubles: 1.34887e+09 deltaJ: 1.08409e+06
Time match with doubles: 1.34887e+09 deltaJ: 2.16587e+06
Time match with time_t: 1348873007 deltaJtimeT: 5.36928e+06
Time match with doubles: 1.34887e+09 deltaJ: 1.08696e+06
Time match with time_t: 1348873008 deltaJtimeT: 1.08696e+06
Time match with doubles: 1.34887e+09 deltaJ: 1.08534e+06
Time match with doubles: 1.34887e+09 deltaJ: 3.18296e+06
...
Time match with time_t: 1348873122 deltaJtimeT: 2.16217e+06
Time match with doubles: 1.34887e+09 deltaJ: 6.42553e+06
Time match with doubles: 1.34887e+09 deltaJ: 1.08727e+06
Time match with doubles: 1.34887e+09 deltaJ: 2.14147e+06
Time match with doubles: 1.34887e+09 deltaJ: 1.04733e+06
Time match with time_t: 1348873130 deltaJtimeT: 8.42965e+06
Note: I am using the deltaJ
output to show a bit more numerically what is a clear visual difference in terms of when the console actually outputs results from this loop.
Clearly, this does not seem to consistently be returning the number of seconds since epoch, as I would instead be able to see a very consistent output of very close to 1 line for both double
and time_t
per second. Instead, I miss the entire range between 1348873002
and 1348873007
- none of these values appear to be found via time(NULL)
. These gaps appear consistently throughout the entire runtime of this loop.
Additionally, sometimes it does not even output for some of the seconds (see the gap between 1348873122
and 1348873130
).
I don't understand why the output doesn't show one entry for both variable types, for each second of real time. Instead, it seems time(NULL)
inconsistently returns the number of seconds since epoch, and actually skips some values.
What am I missing?
I am running these tests on a 2.4GHz dualcore Macbook Pro, in X-Code 3.2.5, on Snow Leopard (perhaps this issue is specific to my system)?
Clearly, this does not seem to consistently be returning the number of seconds since epoch, I would instead be able to see a very consistent output of very close to 1 line for both
double
andtime_t
per second.
This doesn't follow. I think you intended for your code to be something a bit different than it actually is.
Specifically, I think you need to change this:
//run indefinitely... use a debugger you can stop :-)
for (double j = 0; j < 2000000000; j++) {
//get the current time
curT = time(NULL);
curTtimeT = time(NULL);
to this:
//get the current time
curT = time(NULL);
curTtimeT = time(NULL);
//run indefinitely... use a debugger you can stop :-)
for (double j = 0; j < 2000000000; j++) {
because as it is, you have a sort of race condition: you're testing if the return-value of time
changes during certain portions of the loop. Obviously that won't happen 100% of the time; sometimes the second will roll over during one part of the loop, and other times it will roll over during a different part.