public static void main(String[] args) {
long begin = System
long current;
while (begin == (current = System
;
System
}
System
言歸正傳
package cn
/**
* A Timer class uses native methods to measure times
*
* @author pan
*/
public class Timer {
private long prev;
public void reset() {
prev = QueryPerformanceCounter();
}
/**
* @return the duration in ms from the point of reset()
*/
public double getDuration() {
long current = QueryPerformanceCounter();
return (current
}
static final double frequency;
static native long QueryPerformanceFrequency();
static native long QueryPerformanceCounter();
static {
System
frequency = QueryPerformanceFrequency() /
}
}
Native的代碼
#include
#include <windows
JNIEXPORT jlong JNICALL
Java_cn_pandaoen_timer_Timer_QueryPerformanceFrequency(JNIEnv *e
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return (jlong)frequency
}
JNIEXPORT jlong JNICALL
Java_cn_pandaoen_timer_Timer_QueryPerformanceCounter(JNIEnv *e
{
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return (jlong)counter
}
用法是
下面我們來看看這個計時器都多高的精度
public class TimerTest {
public static void main(String[] args) {
long f = Timer
long p = Timer
long c;
while (p == (c = Timer
;
System
}
}
在同樣的系統下
這種方法的一個缺點當然是
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25828.html