The title is pretty much self-explanatory, I'm killing myself over this simplicity.
Looked here, but it isn't much helpful.
nanoTime() method returns the current value of the most precise available system timer, in nanoseconds. The value returned represents nanoseconds since some fixed but arbitrary time (in the future, so values may be negative) and provides nanosecond precision, but not necessarily nanosecond accuracy.
nanoTime() is a great function, but one thing it's not: accurate to the nanosecond. The accuracy of your measurement varies widely depending on your operation system, on your hardware and on your Java version. As a rule of thumb, you can expect microsecond resolution (and a lot better on some systems).
nanoTime. Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds. This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.
I think that the Stopwatch class is what you are looking for.
If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least):
Java:
java.lang.System.nanoTime();
C GNU/Linux:
static int64_t hpms_nano() {
struct timespec t;
clock_gettime( CLOCK_MONOTONIC, &t );
int64_t nano = t.tv_sec;
nano *= 1000;
nano *= 1000;
nano *= 1000;
nano += t.tv_nsec;
return nano;
}
C Windows:
static int64_t hpms_nano() {
static LARGE_INTEGER ticksPerSecond;
if( ticksPerSecond.QuadPart == 0 ) {
QueryPerformanceFrequency( &ticksPerSecond );
}
LARGE_INTEGER ticks;
QueryPerformanceCounter( &ticks );
uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
nano *= 100UL;
return nano;
}
C#:
private static long nanoTime() {
long nano = 10000L * Stopwatch.GetTimestamp();
nano /= TimeSpan.TicksPerMillisecond;
nano *= 100L;
return nano;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With