我想知道的是在更新我的对象在游戏中的位置时是否应该使用System.currentTimeMillis()或System.nanoTime()?它们的运动变化与自上次呼叫后经过的时间成正比,我希望尽可能精确.
我读过不同的操作系统之间存在一些严重的时间分辨率问题(即Mac/Linux的分辨率几乎为1毫秒,而Windows的分辨率为50毫秒).我主要在Windows上运行我的应用程序,50ms分辨率似乎非常不准确.
有没有比我列出的两个更好的选择?
有什么建议/意见吗?
我想要这样的东西:
public class Stream
{
public startTime;
public endTime;
public getDuration()
{
return startTime - endTime;
}
}
Run Code Online (Sandbox Code Playgroud)
同样重要的是,例如,如果startTime为23:00,endTime为1:00,则持续时间为2:00.
在Java中使用哪些类型来实现这一目的?
我希望计算API返回值所花费的时间.这种行动所花费的时间是纳秒秒.由于API是C++类/函数,我使用timer.h来计算相同的:
#include <ctime>
#include <cstdio>
using namespace std;
int main(int argc, char** argv) {
clock_t start;
double diff;
start = clock();
diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
cout<<"printf: "<< diff <<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以秒为单位的时间.如何在毫秒秒内获得相同的精度?
你如何计算java程序的执行时间?我不确定我应该用什么课来做这件事.
我有点像找东西:
// Some timer starts here
for (int i = 0; i < length; i++) {
// Do something
}
// End timer here
System.out.println("Total execution time: " + totalExecutionTime);
Run Code Online (Sandbox Code Playgroud) http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#currentTimeMillis()说:
以毫秒为单位返回当前时间.请注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,并且可能更大.例如,许多操作系统以几十毫秒为单位测量时间.
我不清楚我是否保证这段代码将始终打印不断增加(或相同)的数字.
while (1) {
System.out.println(System.currentTimeMillis() );
}
Run Code Online (Sandbox Code Playgroud) 使用Java Timer,然后切换到ScheduledExecutorService,但我的问题没有修复.在系统时间更改之前安排的任务(通过ntpd)不会在指定的延迟时执行.没有任何日志记录:(.
在64位linux上使用我的目标中的jre 1.6.0_26 64位.
更新: ScheduledExecutorService在Windows上运行正常.问题仅出在运行64位JVM的基于64位Linux的系统上.它在运行32位JVM的64位linux上运行良好......很奇怪.在任何博客上都没有找到任何相同的参考.
IBM的JAVA SDK也存在同样的问题(ibm-java-sdk-7.0-0.0-x86_64-archive.bin).
我已经提交了针对JDK 7139684的缺陷,它被接受但已被关闭并标记为 6900441的副本.请投票给它,如果你觉得它的价值得到修复...我不知道为什么它已经修复了几年以上
以下是我用来测试此问题的示例代码:
package test;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author yogesh
*
*/
public class TimerCheck implements Runnable {
ScheduledExecutorService worker;
public TimerCheck(ScheduledExecutorService worker) {
super();
this.worker = worker;
this.worker.schedule(this, 1, TimeUnit.SECONDS);
}
private static void update() {
System.out.println("TimerCheck.update() "+new Date(System.currentTimeMillis()));
}
@Override
public void run() {
update();
worker.schedule(this, 1, TimeUnit.SECONDS);
}
/**
* @param args
*/
public static void …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码System.nanoTime()来测量代码的已用时间.
public static void main(String[] args) throws Exception {
while (true) {
long start = System.nanoTime();
for (int i = 0; i < 10000; i++)
;
long end = System.nanoTime();
long cost = end - start;
if (cost < 0) {
System.out.println("start: " + start + ", end: " + end + ", cost: " + cost);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了这样的结果:
start: 34571588742886, end: 34571585695366, cost: -3047520
start: 34571590239323, end: 34571586847711, cost: -3391612
start: 34571651240343, end: 34571648928369, cost: …Run Code Online (Sandbox Code Playgroud) 我想计算两个事件之间经过的时间,以纳秒为单位.要做到这一点,我可以使用这里System.nanoTime()提到的.问题是这两个事件发生在不同的线程中.
由于nanoTime()不返回绝对时间戳,而只能用于计算时间差,我想知道我在两个不同线程上获得的值是否与两个事件之间经过的物理时间一致.
我和我这个时代的另一位开发人员最近从一台Core 2 Duo机器搬到了新的Core 2 Quad 9505; 两者都使用JDK 1.6.0_18运行Windows XP SP3 32位.
在这样做的时候,我们对一些时序/统计/度量聚合代码的自动单元测试很快就会失败,因为看起来似乎是从System.nanoTime()返回的荒谬值.
在我的机器上可靠地显示此行为的测试代码是:
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.Test;
public class NanoTest {
@Test
public void testNanoTime() throws InterruptedException {
final long sleepMillis = 5000;
long nanosBefore = System.nanoTime();
long millisBefore = System.currentTimeMillis();
Thread.sleep(sleepMillis);
long nanosTaken = System.nanoTime() - nanosBefore;
long millisTaken = System.currentTimeMillis() - millisBefore;
System.out.println("nanosTaken="+nanosTaken);
System.out.println("millisTaken="+millisTaken);
// Check it slept within 10% of requested time
assertThat((double)millisTaken, Matchers.closeTo(sleepMillis, sleepMillis * 0.1));
assertThat((double)nanosTaken, Matchers.closeTo(sleepMillis * 1000000, sleepMillis * 1000000 …Run Code Online (Sandbox Code Playgroud) 根据其文档,System.nanoTime返回 纳秒,因为一些固定但任意的原始时间.但是,在所有x64机器上我尝试了下面的代码,有时间跳转,移动固定的原始时间.我的方法可能存在一些缺陷,使用替代方法(此处为currentTimeMillis)获取正确的时间.但是,测量相对时间(持续时间)的主要目的也会受到负面影响.
我在比较不同队列到LMAX的Disruptor时试图测量延迟时遇到了这个问题,我有时会出现非常负的延迟.在这些情况下,开始和结束时间戳由不同的线程创建,但延迟是在这些线程完成后计算的.
我的代码在这里花费时间使用nanoTime,计算currentTimeMillis时间中的固定原点,并比较调用之间的原点.因为我必须在这里提出一个问题:这段代码有什么问题?为什么会发现违反固定原产地合同的行为?或者不是吗?
import java.text.*;
/**
* test coherency between {@link System#currentTimeMillis()} and {@link System#nanoTime()}
*/
public class TimeCoherencyTest {
static final int MAX_THREADS = Math.max( 1, Runtime.getRuntime().availableProcessors() - 1);
static final long RUNTIME_NS = 1000000000L * 100;
static final long BIG_OFFSET_MS = 2;
static long startNanos;
static long firstNanoOrigin;
static {
initNanos();
}
private static void initNanos() {
long millisBefore = System.currentTimeMillis();
long millisAfter;
do {
startNanos = System.nanoTime();
millisAfter = System.currentTimeMillis();
} …Run Code Online (Sandbox Code Playgroud)