S******1 发帖数: 269 | 1 为啥外层循环小内层循环大的结构快呢?
public static void main(String[] args){
long z;
Date a=new Date();
for(long i=0; i<10;i++)
for(long j=0;j<1000000000;j++)
z=i*j;
Date b=new Date();
System.out.println(b.getTime()-a.getTime());
}
public static void main(String[] args){
long z;
Date a=new Date();
for(long i=0; i<1000000000;i++)
for(long j=0;j<10;j++)
z=i*j;
Date b=new Date();
System.out.println(b.getTime()-a.getTime());
} | x*********n 发帖数: 46 | 2 because in the inner loop, you have a temporary variable j, it will be "
newed" and "deleted" by 10 times at the first situation, while it will do
the same by 10000000 times at the second situation. | S******1 发帖数: 269 | 3 明白了,谢谢你!
【在 x*********n 的大作中提到】 : because in the inner loop, you have a temporary variable j, it will be " : newed" and "deleted" by 10 times at the first situation, while it will do : the same by 10000000 times at the second situation.
|
|