相关疑难解决方法(0)

为什么第一次nanoTime()调用和连续调用之间的时间差异如此之大?

所以我的问题更为笼统.我有以下简单的代码:

for(int i=0;i<10;i++){
    long starttime=System.nanoTime();
    System.out.println("test");
    long runtime=System.nanoTime()-starttime;
    System.out.println(i + ":" +"runtime="+runtime);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下输出:

test
0:runtime=153956
test
1:runtime=15396
test
2:runtime=22860
test
3:runtime=11197
test
4:runtime=11197
test
5:runtime=12129
test
6:runtime=11663
test
7:runtime=11664
test
8:runtime=53185
test
9:runtime=12130
Run Code Online (Sandbox Code Playgroud)

第一个和第二个运行时区别的原因是什么?提前感谢=)

java system nanotime

14
推荐指数
2
解决办法
1409
查看次数

为什么对同一方法的两次连续调用会产生不同的执行时间?

这是一个示例代码:

public class TestIO{
public static void main(String[] str){
    TestIO t = new TestIO();
    t.fOne();
    t.fTwo();
    t.fOne();
    t.fTwo();
}


public void fOne(){
    long t1, t2;
    t1 = System.nanoTime();
    int i = 10;
    int j = 10;
    int k = j*i;
    System.out.println(k);
    t2 = System.nanoTime();
    System.out.println("Time taken by 'fOne' ... " + (t2-t1));
}

public void fTwo(){
    long t1, t2;
    t1 = System.nanoTime();
    int i = 10;
    int j = 10;
    int k = j*i;
    System.out.println(k);
    t2 = System.nanoTime();
    System.out.println("Time taken …
Run Code Online (Sandbox Code Playgroud)

java benchmarking microbenchmark

2
推荐指数
3
解决办法
1632
查看次数

标签 统计

java ×2

benchmarking ×1

microbenchmark ×1

nanotime ×1

system ×1