代码
public class ForTest {
List<String> list = new ArrayList<String>();
public void setList() {
for (int i = 0 ; i < 10000; i++) {
list.add("hello world "+ LocalDateTime.now());
}
}
public void printListBySize() {
for (int i = 0 ; i < this.list.size(); i++) {
System.out.println(list.get(i));
}
}
public void printListByIncresment() {
for (String str : this.list) {
System.out.println(str);
}
}
public void printListByIterator() {
Iterator iter = this.list.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
}
public void printListByLambda() {
list.forEach(item->System.out.println(item));
}
public void printListByForEach() {
list.forEach(System.out::println);
}
public void printListByStream() {
list.stream().forEach(System.out::println);
}
}
编译后的class
public class ForTest {
List<String> list = new ArrayList();
public ForTest() {
}
public void setList() {
for(int i = 0; i < 10000; ++i) {
this.list.add("hello world " + LocalDateTime.now());
}
}
public void printListBySize() {
for(int i = 0; i < this.list.size(); ++i) {
System.out.println((String)this.list.get(i));
}
}
public void printListByIncresment() {
Iterator var1 = this.list.iterator();
while(var1.hasNext()) {
String str = (String)var1.next();
System.out.println(str);
}
}
public void printListByIterator() {
Iterator iter = this.list.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
public void printListByLambda() {
this.list.forEach((item) -> {
System.out.println(item);
});
}
public void printListByForEach() {
List var10000 = this.list;
PrintStream var10001 = System.out;
System.out.getClass();
var10000.forEach(var10001::println);
}
public void printListByStream() {
Stream var10000 = this.list.stream();
PrintStream var10001 = System.out;
System.out.getClass();
var10000.forEach(var10001::println);
}
}
jmh测试代码
预热5次,迭代测试5次
public static void main(String[] args) throws RunnerException {
Options opt = (new OptionsBuilder()).include(SecondBenchmark.class.getSimpleName()).forks(1).warmupIterations(5).measurementIterations(5).build();
(new Runner(opt)).run();
}
测试结果:
Benchmark (length) Mode Cnt Score Error Units
SecondBenchmark.printListByForEach 10000 avgt 5 3228874.149 ± 550041.812 us/op
SecondBenchmark.printListByForEach 100000 avgt 5 2490442.832 ± 320280.382 us/op
SecondBenchmark.printListByForEach 1000000 avgt 5 2948198.976 ± 214978.457 us/op
SecondBenchmark.printListByIncresment 10000 avgt 5 2322080.870 ± 127456.215 us/op
SecondBenchmark.printListByIncresment 100000 avgt 5 3287660.659 ± 397783.792 us/op
SecondBenchmark.printListByIncresment 1000000 avgt 5 3041442.821 ± 316420.454 us/op
SecondBenchmark.printListByIterator 10000 avgt 5 3695825.938 ± 121482.650 us/op
SecondBenchmark.printListByIterator 100000 avgt 5 2601730.372 ± 146823.180 us/op
SecondBenchmark.printListByIterator 1000000 avgt 5 3115649.546 ± 174462.350 us/op
SecondBenchmark.printListByLambda 10000 avgt 5 3877676.612 ± 202146.484 us/op
SecondBenchmark.printListByLambda 100000 avgt 5 2873856.351 ± 515957.764 us/op
SecondBenchmark.printListByLambda 1000000 avgt 5 2511439.192 ± 320532.925 us/op
SecondBenchmark.printListBySize 10000 avgt 5 3268756.720 ± 218007.113 us/op
SecondBenchmark.printListBySize 100000 avgt 5 3340940.641 ± 207688.768 us/op
SecondBenchmark.printListBySize 1000000 avgt 5 2263252.726 ± 102699.753 us/op
SecondBenchmark.printListByStream 10000 avgt 5 2304141.941 ± 344516.326 us/op
SecondBenchmark.printListByStream 100000 avgt 5 2458426.005 ± 237666.833 us/op
SecondBenchmark.printListByStream 1000000 avgt 5 2363297.047 ± 323942.425 us/op