4

Good morning,

i need to obtain the value of the CPUs high-resolution performance counter in order to measure delay between various software applications. In my c(++|#) i use the

BOOL WINAPI QueryPerformanceCounter( __out LARGE_INTEGER *lpPerformanceCount );

WinApi call. What is the proper way to obtain the counter from java. I have search jna without success. I know this is a platform specific issue, but maybe there is a quicker way than writing my own jni wrapper?

Best wishes, Armin

2 Answers 2

4

How about using System.nanoTime? I think that already uses the performance counters of the machine and there is no need to write a native wrapper.

Update: According to this article on clocks and timers in the jvm in the section "Clocks and Timers on Windows"

System.nanoTime() is implemented using the QueryPerformanceCounter/QueryPerformanceFrequency API

Sign up to request clarification or add additional context in comments.

5 Comments

Jörn, nanoTime() returns the time, rather than the uncooked performance counter. Since i want to measure delay between different applications i need the performance counter value rather than a transform.
nanoTime returns counter, not time. Read javadoc.
@Stas No, it returns the number of seconds the PC has been running, read the implementation: hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/0cd040567d60/src/…
@Motes "QueryPerformanceCounter" doesn't look like has anything with seconds. And "QueryPerformanceFrequency" which used to define "has_performance_count" has the following on MSDN: "On systems that run Windows XP or later, the function will always succeed and will thus never return zero.", which means that has_performance_count is always true.
The code returns jlong time = (jlong)((current/freq) * NANOSECS_PER_SEC);
2

Here is the native wrapper code

1) File W32Call.java

package jmSense.Native; public class W32Call { public native static long QueryPerformanceCounter( ); public native static int QueryPerformanceCounterInt32( ); 

2) run java h to create the include file

3) Create a dll "My Native Extensions.dll" from

#include "stdafx.h" #include "windows.h" #include "jmSense_Native_W32Call.h" JNIEXPORT jlong JNICALL Java_jmSense_Native_W32Call_QueryPerformanceCounter(JNIEnv *, jclass) { LARGE_INTEGER g_CurentCount; QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); return g_CurentCount.QuadPart; } JNIEXPORT jint JNICALL Java_jmSense_Native_W32Call_QueryPerformanceCounterInt32(JNIEnv *, jclass) { LARGE_INTEGER g_CurentCount; QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); return g_CurentCount.LowPart; } 

4) Use it like this:

System.loadLibrary("My Native Extensions"); System.out.println(W32Call.QueryPerformanceCounter()); 

fine

1 Comment

Why would you do all that? QueryPerformanceCounter is used by System.nanoTime anyway. Just use System.nanoTime.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.