论坛首页 入门技术论坛

一道简单的面试题

浏览 29498 次
该帖已经被评为新手帖
作者 正文
   发表时间:2010-04-05  
乱七八糟的呀。。。这样的优化只能说是“硬”优化吧。。
0 请登录后投票
   发表时间:2010-04-05  
学习了,感觉优化还是有效果的饿,但是如果不是太大的循环的话必要性不是太大
0 请登录后投票
   发表时间:2010-04-05  
laoshifu 写道
实例化次数计算不对,对于java来说,for语句的初始化只是执行一次。所以对于原代码,优化一和优化二实例化变量的实例化次数都是一。不对之处,多多指教。


for语句的初始化只是执行一次,循环体执行不只一次,所以第2、3层循环不只初始化一次。
0 请登录后投票
   发表时间:2010-04-06  
for (int i = 0; i < 1000; i++) 
   for (int j = 0; j < 100; j++) 
       for (int k = 0; k < 10; k++) 
           log(i * j * k); 

i*j*k 当i=0 或者j=0, 或者k=0时,,都相当于log(0),,也就是log(0)重复计算了1000×100×10次,,,类似的重复计算太多

优化点应该是把log的重复计算降低
0 请登录后投票
   发表时间:2010-04-06  
zhangkaitao 写道
测试机器 CPU P8700 @ 2.53GHZ  MEMORY 2GB

public static void testFor0() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 100; i++) 
            for (j = 1; j <= 1000; j++)
                for (k = 1; k <= 10000; k++) {
                    count++;
                }
        System.out.println("zero count==" + count);
        long endTime = System.nanoTime();
        System.out.println("zero time==== " + (endTime - startTime));
    }
    
    public static void testFor1() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 10000; i++) 
            for (j = 1; j <= 1000; j++)
                for (k = 1; k <= 100; k++) {
                    count++;
                }
        System.out.println("first count==" + count);
        long endTime = System.nanoTime();
        System.out.println("first time==== " + (endTime - startTime));
    }
    
    public static void testFor2() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 100; i++) 
            for (j = i; j <= 1000; j++)
                for (k = j; k <= 10000; k++) {
                    count++;
                }
        System.out.println("second count==" + count);
        long endTime = System.nanoTime();
        System.out.println("second time==== " + (endTime - startTime));
    }
    
    public static void testFor3() {
        int ii = 10000, jj = 1000, kk = 100, sum = ii * jj * kk, iii = 0, kkk = 0, jjj = 0;  
        int count = 0;
        long startTime = System.nanoTime();
        for (ii = 0, jj = 0, kk = 0; kk < sum; kk++) {  
            kkk = kk % 100;  
            if (kkk == 0 && kk > 0) {  
                jj++;  
                jjj = jj % 1000;  
                if (jjj == 0) {  
                    ii++;  
                    iii = ii%10000;  
                }
            }
            count++;
        }
        System.out.println("third count==" + count);
        long endTime = System.nanoTime();
        System.out.println("third time==== " + (endTime - startTime));
    }



结果

zero count==1000000000
zero time==== 858950788
first count==1000000000
first time==== 941317961
second count==900711700
second time==== 756062573
third count==1000000000
third time==== 6520663101


    public static void testFor0() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 10; i++) 
            for (j = 1; j <= 100; j++)
                for (k = 1; k <= 1000; k++) {
                    count++;
                }
        System.out.println("zero count==" + count);
        long endTime = System.nanoTime();
        System.out.println("zero time==== " + (endTime - startTime));
    }
    
    public static void testFor1() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 1000; i++) 
            for (j = 1; j <= 100; j++)
                for (k = 1; k <= 10; k++) {
                    count++;
                }
        System.out.println("first count==" + count);
        long endTime = System.nanoTime();
        System.out.println("first time==== " + (endTime - startTime));
    }
    
    public static void testFor2() {
        int count = 0;
        int i = 1;
        int j = 1;
        int k = 1;
        long startTime = System.nanoTime();
        for (; i <= 10; i++) 
            for (j = i; j <= 100; j++)
                for (k = j; k <= 1000; k++) {
                    count++;
                }
        System.out.println("second count==" + count);
        long endTime = System.nanoTime();
        System.out.println("second time==== " + (endTime - startTime));
    }
    
    public static void testFor3() {
        int ii = 1000, jj = 100, kk = 10, sum = ii * jj * kk, iii = 0, kkk = 0, jjj = 0;  
        int count = 0;
        long startTime = System.nanoTime();
        for (ii = 0, jj = 0, kk = 0; kk < sum; kk++) {  
            kkk = kk % 10;  
            if (kkk == 0 && kk > 0) {  
                jj++;  
                jjj = jj % 100;  
                if (jjj == 0) {  
                    ii++;  
                    iii = ii%1000;  
                }
            }
            count++;
        }
        System.out.println("third count==" + count);
        long endTime = System.nanoTime();
        System.out.println("third time==== " + (endTime - startTime));
    }


结果
zero count==1000000
zero time==== 1811683
first count==1000000
first time==== 2361474
second count==905620
second time==== 2054730
third count==1000000
third time==== 7406249

大家看看结果吧。



有理有据,看样子10  100 1000的循环优化还是有效果的。
0 请登录后投票
   发表时间:2010-04-06  
听说递减效率更好
0 请登录后投票
   发表时间:2010-04-06  
怎么还不如原来的了呢.......
0 请登录后投票
   发表时间:2010-04-06  
1.i,j,k必须从1开始

2.i*j*k是有重复的

3.log(i*j*k)=logi+logj+logk

这才是这道题的关键吧,像这种初始化的东西,根本相差不大
0 请登录后投票
   发表时间:2010-04-06  
int i, j, k, m;  
for (i = 0; i < 10; i++){
   for (j = 0; j < 100; j++){
	   m=i*j;
	   for (k = 0; k < 100; k++){
		   log(m * k);
	   }
   }
}
0 请登录后投票
   发表时间:2010-04-06  
感觉差不多,现在的服务器一般内存都很大,不会在乎那么一丁点的损耗
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics