本帖最后由 李哲 于 2012-5-29 09:56 编辑
直觉是第二种。{:soso_e100:}
但是经过测试,似乎直觉不靠谱。
第一种- public class ForCompare {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- long all=0;
- long allTime=0;
- while(all<50){
- int v1=100;
- int v2=10;
- int v3=1;
- long l2=System.currentTimeMillis();
-
-
- for(int i=0; i<v1;i++){
- for(int j=0; j<v2; j++){
- for(int k=0; k<v3; k++){
-
- AAA();
- }
- }
- }
- long l3=System.currentTimeMillis();
-
- System.out.println(l3 - l2);
- all++;
- allTime+=l3 - l2;
-
- }
- System.out.println(allTime);
- //51906
- //593
- //250,266,266,250,250
- //1062,1172,1094,1125,1062,1094,1047,1110,1063,1156,1093
- }
- public static void AAA(){
- for (int i = 0; i < 2; i++) {
- System.out.println("hehe");
-
- }
- }
- }
复制代码 第二种- public class CopyOfForCompare1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
-
- long all=0;
- long allTime=0;
- while(all<50){
- int v1=100;
- int v2=10;
- int v3=1;
- long l2=System.currentTimeMillis();
- for(int i=0; i<v3;i++){
- for(int j=0; j<v2; j++){
- for(int k=0; k<v1; k++){
-
- AAA();
- }}}
- long l3=System.currentTimeMillis();
-
- System.out.println(l3 - l2);
- all++;
- allTime+=l3 - l2;
-
- }
- System.out.println(allTime);
- //54234
- //625
- //234,265,250,266,266
- //1141,1125,1156,1109,1094,1141,1156,1141,1031,1109
- }
- public static void AAA(){
- for (int i = 0; i < 2; i++) {
- System.out.println("hehe");
-
- }
- }
- }
复制代码 下面的那些注释为修改v1,v2,v3,all之后得到的结果。
|