I would like to print the nanotimes from two languages and compare the value.
JAVA code
long nano_startTime = System.nanoTime();
System.out.println(nano_startTime);
C++ code
system_clock::time_point begin = system_clock::now();
auto since_epoch = begin.time_since_epoch(); // get the duration since epoch
std::cout << std::to_string(duration_cast<nanoseconds>(since_epoch).count()) << std::endl;
I expect the results to be equivalent but the results are different...!
Results
JAVA: 4459739378141
C++ 1584649009920663623
BTW: If i use milliseconds the results are equivalent. But I require more precision time data and unfortunately microseconds is not there in java.
Can someone help me? Thanks.
Read the Javadoc of System.nanoTime()
(emphasis added):
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
On the other hand, in your C++ code, since_epoch
is the time, well, since epoch (presuming the method was not named capriciously; you could check its documentation too).
And System.currentTimeMillis()
returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
So that returns the time since epoch as well.
Documentation is a good place to start to understand the behavior of methods.