近幾天,看老大的框架,里面出現這樣的語句for (int i = 0, size = c.size(); i < size; i++),我總覺得這個和for (int i = 0; i < c.size(); i++)沒有什么不同,畢竟java不是C,一般情況下,我們程序給運行了,底層JVM應該給我們優化才是。于是,我寫了個程序對比了一下,發現性能情況還是很大不同的。
JVM到底有沒有優化呢。我沒有打開匯編看。但至少我得出結論:for (int i = 0, size = c.size(); i < size; i++)這種寫法沒有必要。數據量不大的時候,反而效率低很多。
各位高手給建議。。。。。。。Why
package com.wang.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Test {
private final static int Length = 1000;
public static void main(String[] args) {
int length = 1000;
List<Double> c = new ArrayList<Double>();
initSet(c);
long t1 = System.currentTimeMillis();
test1(c);
long t2 = System.currentTimeMillis();
test2(c);
long t3 = System.currentTimeMillis();
test3(c);
long t4 = System.currentTimeMillis();
test4(c);
long t5 = System.currentTimeMillis();
System.out
.println("*******************************************************************");
System.out.println(t2 - t1);
System.out.println(t3 - t2);
System.out.println(t4 - t3);
System.out.println(t5 - t4);
}
private static void test3(List<Double> c) {
for (Iterator<Double> iterator = c.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
}
private static void test2(List<Double> c) {
for (int i = 0; i < c.size(); i++) {
System.out.println(c.get(i));
}
}
private static void test4(List<Double> c) {
for (int i = c.size() - 1; i >= 0; i--) {
System.out.println(c.get(i));
}
}
private static void test1(List<Double> c) {
for (int i = 0, size = c.size(); i < size; i++) {
System.out.println(c.get(i));
}
}
private static void initSet(List<Double> c) {
for (int i = 0; i < Length; i++) {
c.add(Math.random());
}
}
}
Length==1000時:import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Test {
private final static int Length = 1000;
public static void main(String[] args) {
int length = 1000;
List<Double> c = new ArrayList<Double>();
initSet(c);
long t1 = System.currentTimeMillis();
test1(c);
long t2 = System.currentTimeMillis();
test2(c);
long t3 = System.currentTimeMillis();
test3(c);
long t4 = System.currentTimeMillis();
test4(c);
long t5 = System.currentTimeMillis();
System.out
.println("*******************************************************************");
System.out.println(t2 - t1);
System.out.println(t3 - t2);
System.out.println(t4 - t3);
System.out.println(t5 - t4);
}
private static void test3(List<Double> c) {
for (Iterator<Double> iterator = c.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
}
private static void test2(List<Double> c) {
for (int i = 0; i < c.size(); i++) {
System.out.println(c.get(i));
}
}
private static void test4(List<Double> c) {
for (int i = c.size() - 1; i >= 0; i--) {
System.out.println(c.get(i));
}
}
private static void test1(List<Double> c) {
for (int i = 0, size = c.size(); i < size; i++) {
System.out.println(c.get(i));
}
}
private static void initSet(List<Double> c) {
for (int i = 0; i < Length; i++) {
c.add(Math.random());
}
}
}
94
62
32
31
Length==10000時:62
32
31
516
406
375
344
Length==100000時:406
375
344
3563
3453
3641
3453
Length==1000000時:3453
3641
3453
35109
34625
36469
34891
34625
36469
34891
JVM到底有沒有優化呢。我沒有打開匯編看。但至少我得出結論:for (int i = 0, size = c.size(); i < size; i++)這種寫法沒有必要。數據量不大的時候,反而效率低很多。
各位高手給建議。。。。。。。Why