java - 为什么 test1() 比 test2() 运行得快得多?

标签 java performance profile

import java.util.Random;


public class Test{
    static int r = new Random().nextInt(2);
    static int a(){
        return r==1 ? 1 :0;
    }

    public static void test1() throws  Exception {
        //
        System.out.println(1403187139018L);
        for (int i = 0; i <   1073741824; i++) {}//*

        // Thread.sleep(20000);
        long d = 0;

        for (int j = 0; j < 10; j++) {
            long y = System.currentTimeMillis();

            for (int x = 0; x < 1073741823; x++) {
                d += r==0?1:0;
            }
            System.out.println((System.currentTimeMillis() -y));
        }
    }

    public static void test2()  throws  Exception{

        // Thread.sleep(20000);
        long d = 0;

        for (int j = 0; j < 10; j++) {
            long y = System.currentTimeMillis();

            for (int x = 0; x < 1073741824; x++) {
                d += r==0?1:0;
            }
            System.out.println((System.currentTimeMillis() -y));

            // System.out.println("time:"+ (System.currentTimeMillis() - y));
        }
    }

    public static void main(String[] args) throws  Exception{
        // Thread.sleep(20000);

        test1();
        test2();

    }

}

当我运行上面的代码时,我得到以下输出:

32
26
28
28
32
29
35
33
30
31
1321
1308
1324
1277
1348
1321
1337
1413
1287
1331

为什么 test1 更快?

除了以下几点之外没有任何区别:

System.out.println(1403187139018L);
for (int i = 0; i <   1073741824; i++) {}//*

此外,test1 的时间成本为 25-35 毫秒,这让我难以置信。我用 C 语言编写了相同的代码,运行每个 for 循环大约需要 4 秒。

这种行为看起来很奇怪。我如何知道何时添加:

System.out.println(1403187139018L);
for (int i = 0; i <   1073741824; i++) {}//*

另外,如果我改变

r==0?1:0

a()

然后 test2() 比 test1() 运行得更快。

我得到的输出是:

1403187139018
3726
3729
3619
3602
3797
4362
4498
3816
4143
4368
1673
1386
1388
1323
1296
1337
1294
1283
1235
1460

原始遗留代码: ...

long t = System.currentTimeMillis();
MappedByteBuffer mbb = map(new File("temp.mmp"), 1024L * 1024 * 1024);

System.out.println("load " + (System.currentTimeMillis() - t));//*
for (int i = 0; i < 2014L * 1024 * 1024; i++) {}//*
int d = 0;
for (int j = 0; j < 10; j++) {
    t = System.currentTimeMillis();
    mbb.position(0);
    mbb.limit(mbb.capacity());

    for (int i = 0; i < mbb.capacity(); i++) {
        d += mbb.get();
    }

    ....
}
System.out.println(d);

最佳答案

空循环可能会在第一个方法中触发 JIT 编译。而且 JIT 足够聪明,能够意识到你的代码除了获取当前时间和打印时间差之外没有做任何有用的事情。因此,它通过根本不运行无用代码来优化它们。

如果您编写实际有用的代码,JIT 就会做正确的事情。不要试图通过添加空循环来搞乱它。

关于java - 为什么 test1() 比 test2() 运行得快得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24343438/

相关文章:

python - Python cProfile 中的严重开销?

java - 何时使用 @NotNull 和 @Nullable IntelliJ 注释?

java - 为什么每次运行这个程序输出的顺序都不一样?

java - 从 Spring MVC 中的 Controller 调用 Jsp 页面

javascript - 改进 Javascript Mandelbrot 绘图性能

performance - Spark 本地 vs hdfs 性能

automation - 网站如何自动抓取个人资料图片?

macos - 在Mac上的.profile中导出路径

java - LinkedList 有一个名为 Entry 的静态类。为什么它是静态的,有什么好处?

python - MYSQL//"DISTINCT"的性能在减少不同条目数量时下降了大约100000条条目