Java 7排序“优化” [英] Java 7 sorting "optimisation"
问题描述
在Java6中,quicksort和mergesort分别用于 Arrays#sort
,分别用于原始和对象数组。在Java7中,这些都已经改变, DualPivotQuicksort 和Timsort。
In Java6 both quicksort and mergesort were used in Arrays#sort
, for primitive and object arrays respectively. In Java7 these have both changed, to DualPivotQuicksort and Timsort.
在新快速排序的来源中,以下评论会出现在几个地方(例如第354行):
In the source for the new quicksort, the following comment appears in a couple of places (eg line 354):
/*
* Here and below we use "a[i] = b; i++;" instead
* of "a[i++] = b;" due to performance issue.
*/
这是一个性能问题?编译器不会将这些减少到同样的事情吗?
How is this a performance issue? Won't the compiler will reduce these to the same thing?
更广泛地说,自己调查这个的好策略是什么?我可以运行基准测试,但我更感兴趣的是分析编译代码中的任何差异。但是,我不知道使用什么工具等。
More broadly, what is a good strategy for investigating this myself? I can run benchmarks, but I'd be more interested in analysing any differences in the compiled code. However, I don't know what tools to use etc.
推荐答案
这只是一般问题的答案。
This is only an answer to the general question.
您可以查看字节码并尝试理解差异。即你可以用 a [i] = b;写一个简单的例子。 i ++;
和 a [i ++] = b;
,看看差异是什么。
You can look at the bytecode and try to understand the differences. I.e. you could write yourself a simple example using both a[i] = b; i++;
and a[i++] = b;
and see whats the difference.
显示字节码的最简单方法是 javap
程序(应该包含在JDK中)。使用 javac SomeFile.java
编译代码并在代码上运行javap: javap -c SomeFile
( - c开关告诉javap输出文件中每个方法的字节码。)
The simplest way to show the bytecode is the javap
program (should be included in you JDK). Compile the code with javac SomeFile.java
and run javap on the code: javap -c SomeFile
(the -c switch tells javap to output the bytecode for each method in the file).
如果你正在使用eclipse,你也可以试试这一个。
If you are using eclipse you could also try this one.
这篇关于Java 7排序“优化”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!