Reputation: 45
I am trying to measure time in JNI call, it call from native to Java.
In C code, I apply "GetTickCount" api to get JNI execution time and "System.nano" in Java.
Example:
long start = GetTickCount();
....//CallStaticMethod exampleMethod...
long end = GetTickCount();
long duration = end - start;
And in Java
public static exampleMethod(){
long start = System.nano();
//do something
long end = System.nano();
long duration = TimeUnit.NANOSECONDS.toMilis(end - start);
}
Sometimes, it is inconsistent because the java's duration is more than C's duration. ( about 5 to 10 miliseconds)
Upvotes: 3
Views: 1249
Reputation: 98630
GetTickCount
and System.nanoTime()
use different time sources.
The problem with GetTickCount
is its very poor resolution, usually around 10ms. This is explicitly remarked in the documentation:
The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds.
No wonder that it can differ from nanoTime
that much.
To make measurements consistent with System.nanoTime
, use higher resolution timer by calling either QueryPerformanceCounter
or GetSystemTimeAsFileTime
.
EDIT
JDK's System.nanoTime()
is implemented on top of QueryPerformanceCounter
(the source).
Upvotes: 2
Reputation:
On the java side of things you will always be at the mercy of the JVM :
http://shipilev.net/blog/2014/nanotrusting-nanotime/
Upvotes: 0