几种for循环的实现

 

代码

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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值