简体   繁体   English

如何查询高分辨率性能计数器

[英]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. 我需要获取CPU高分辨率性能计数器的值,以便测量各种软件应用程序之间的延迟。 In my c(++|#) i use the 在我的c(++ |#)我使用

BOOL WINAPI QueryPerformanceCounter( __out LARGE_INTEGER *lpPerformanceCount ); BOOL WINAPI QueryPerformanceCounter(__out LARGE_INTEGER * lpPerformanceCount);

WinApi call. WinApi电话。 What is the proper way to obtain the counter from java. 从java获取计数器的正确方法是什么。 I have search jna without success. 我没有成功搜索jna。 I know this is a platform specific issue, but maybe there is a quicker way than writing my own jni wrapper? 我知道这是一个特定于平台的问题,但也许比编写我自己的jni包装更快?

Best wishes, Armin 祝愿,阿明

How about using System.nanoTime ? 使用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" 更新:根据这篇关于jvm中的时钟和计时器的文章“Windows上的时钟和计时器”

System.nanoTime() is implemented using the QueryPerformanceCounter/QueryPerformanceFrequency API System.nanoTime()使用QueryPerformanceCounter / QueryPerformanceFrequency API实现

Here is the native wrapper code 这是本机包装器代码

1) File W32Call.java 1)文件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 2)运行java h来创建包含文件

3) Create a dll "My Native Extensions.dll" from 3)从中创建一个dll“My Native Extensions.dll”

#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: 4)像这样使用它:

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

fine 精细

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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