简体   繁体   中英

How to query the high-resolution performance counter

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM