java -- while循环拆分性能测试程序

来源:互联网 发布:mac下载百度云太慢 编辑:程序博客网 时间:2024/06/09 15:14

public class SimpleTest {

// 包含打印操作//100000  658  1061 1.6//1000000  5576  5073 0.9// 纯加操作//100000 4 6 1-1.5//100000 15 27 1.8//10000000 33 66 2.0public static void main(String[] args) {    final int N = 10000000;    List<Integer> list = new ArrayList<>();    for (int i = 0; i < N; i++) {        list.add(i);    }    TimeWatcher watcher = new TimeWatcher();    long sum1 = 0;    long sum11 = 0;    Iterator<Integer> it = list.iterator();    while (it.hasNext()) {        int i = it.next();        sum11 += i;        sum1 += i;    }    long firstInterval = watcher.interval();    long sum2 = 0;    long sum22 = 0;    Iterator<Integer> it2 = list.iterator();    while (it2.hasNext()) {        int i = it2.next();        sum2 += i;    }    it2 = list.iterator();    while (it2.hasNext()) {        int i = it2.next();        sum22 += i;    }    long secondInterval = watcher.interval();    System.out.println(firstInterval);    System.out.println(secondInterval);    System.out.println(1.0 * secondInterval / firstInterval);}

}