尝试计算时间差以比较两个排序算法。

huangapple go评论93阅读模式
英文:

Trying to compute the time difference to compare two sorting algorithms

问题

public static void main(String[] args){
    int[] nums;
    int n = 1000000;
    int m = 1000;
    List<Long> results = new ArrayList<Long>();
    Instant before, after;
    long delta;

    // for(int i = 0; i < 5; i++){
    //     nums = IntStream.rangeClosed(1, n).toArray();
    //     shuffle(nums);
    //     before = Instant.now(); 
    //     findKthSmallest(nums, m);
    //     quickSort(nums,0 , m-1);    
    //     after = Instant.now();
    //     delta = Duration.between(before, after).toMillis();
    //     System.out.println(delta);
    // }

    nums = IntStream.rangeClosed(1, n).toArray();
    shuffle(nums);
    before = Instant.now(); 
    findKthSmallest(nums, m);
    quickSort(nums,0 , m-1);    
    after = Instant.now();
    delta = Duration.between(before, after).toMillis();
    System.out.println(delta);
}

在主方法中,我尝试打印出 delta 值 5 次。
当我在循环内部执行时(被注释的代码块),delta 值随时间减少,这很奇怪。
当我在循环外部执行时(运行代码 5 次),delta 值似乎是一致的。
为什么会发生这种情况,我该如何修复?
谢谢。

英文:
public static void main(String[] args){
	int[] nums;
	int n = 1000000;
    int m = 1000;		
	List&lt;Long&gt; results = new ArrayList&lt;Long&gt;();
    Instant before, after;
    long delta;

    // for(int i = 0; i &lt; 5; i++){
    //     nums = IntStream.rangeClosed(1, n).toArray();
    //     shuffle(nums);
    //     before = Instant.now(); 
    //     findKthSmallest(nums, m);
    //     quickSort(nums,0 , m-1);    
    //     after = Instant.now();
    //     delta = Duration.between(before, after).toMillis();
    //     System.out.println(delta);
    // }

    nums = IntStream.rangeClosed(1, n).toArray();
    shuffle(nums);
    before = Instant.now(); 
    findKthSmallest(nums, m);
    quickSort(nums,0 , m-1);    
    after = Instant.now();
    delta = Duration.between(before, after).toMillis();
    System.out.println(delta);
}

In the main method, I tried to print out delta 5 times.
When I did it inside the loop (commented-out block), the delta decreased over time, which is weird.
When I did it outside the loop (run the code 5 times), the deltas seem to be consistent.
Why is this happening and how can I fix it?
Thank you.

答案1

得分: 0

当您编译您的Java代码时,它会编译成Java字节码。当您的程序运行时,它会将字节码解释成机器代码。代码的大部分只运行一次,因此JVM只会解释它一次,不会存储机器代码。当JVM注意到您一遍又一遍地运行相同的代码时,它会将该部分编译成机器代码,并尽力进行优化。这被称为即时编译

您可以禁用这个功能,并使用命令行参数-nojit强制程序始终以解释方式运行,禁用JIT编译器。这将导致性能较差,但更一致。

至于准确测量程序热身后的性能,我建议您确定JVM需要多长时间来热身,然后开始测量。

英文:

When you compile your java code, it compiles to java bytecode. When your program runs, it interprets the bytecode into machine code. Most parts of the code only run once, so the JVM only interprets it once and doesn't store the machine code. When the JVM notices you're running the same code over and over again, it compiles that section down to machine code and tries to optimize as well as it can. This is called Just-in-time compilation.

You can disable this, and force the program to constantly run interpreted with the JIT compiler disabled using the command line argument -nojit. This will lead to worse, but more consistent performance.

As for accurately measuring the warmed-up performance of a program, I suggest you identify how long it takes for the JVM to warm up and start measuring then.

huangapple
  • 本文由 发表于 2020年4月7日 21:32:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61081225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定