zoukankan      html  css  js  c++  java
  • 循环

     1 static class Foo {
     2     int mSplat;
     3 }
     4 
     5 Foo[] mArray = ...
     6 
     7 public void zero() {
     8     int sum = 0;
     9     for (int i = 0; i < mArray.length; ++i) {
    10         sum += mArray[i].mSplat;
    11     }
    12 }
    13 
    14 public void one() {
    15     int sum = 0;
    16     Foo[] localArray = mArray;
    17     int len = localArray.length;
    18 
    19     for (int i = 0; i < len; ++i) {
    20         sum += localArray[i].mSplat;
    21     }
    22 }
    23 
    24 public void two() {
    25     int sum = 0;
    26     for (Foo a : mArray) {
    27         sum += a.mSplat;
    28     }
    29 }

    zero() is slowest, because the JIT can't yet optimize away the cost of getting the array length once for every iteration through the loop.

    one() is faster. It pulls everything out into local variables, avoiding the lookups. Only the array length offers a performance benefit.

    two() is fastest for devices without a JIT, and indistinguishable from one() for devices with a JIT. It uses the enhanced for loop syntax introduced in version 1.5 of the Java programming language.

    学会勇敢
  • 相关阅读:
    网络并发服务器设计
    linux脚本编程技术
    守护进程学习
    UDP通讯程序设计
    TCP通讯程序设计
    linux中socket的理解
    linux网络协议
    kafka ProducerConfig 配置
    crontab定时执行datax
    crontab
  • 原文地址:https://www.cnblogs.com/Sir-Lin/p/7131630.html
Copyright © 2011-2022 走看看