关于for循环效率的问题 那位大神能帮忙解释一下 一般都是外层循环小内层循环大的效率会比外层大内层小快,但是如果用一个循环为什么会比用两个循环慢呢,而且会慢很多 import java.util.Date; public class sssfda { public static void main(String[] args) { long i; long time1=System.nanoTime(); for (i = 0; i < 1000000000; i++) { } long time2=System.nanoTime(); System.out.println(i); System.out.println(time2-time1);
long k = 0; long time3=System.nanoTime(); for (int o = 0; o < 10000000; o++) { for (int b = 0; b < 100; b++) { k = k + 1; } } long time4=System.nanoTime(); System.out.println(k); System.out.println(time4-time3);
long f = 0; long time5=System.nanoTime(); for (int o = 0; o < 100; o++) { for (int b = 0; b < 10000000; b++) { f = f + 1; } } long time6=System.nanoTime(); System.out.println(f); System.out.println(time6-time5); } }