佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1154|回复: 13

抛砖引玉之 performance tune

[复制链接]
发表于 23-10-2008 05:03 PM | 显示全部楼层 |阅读模式
小弟不才, 最近忽发心思, 写了几个关于unnessesary processing 的script, 在这里贴出来, 希望可以引起高手们的热烈回应。
提出一些常犯的错误, 以及如何能够有效的编写程式, 除去某些多余的coding

tune 1 - the comparison tuning :

以array来代替某些if-else的 comparison。


numeric range determination script

This program will determine the numeric range of 15 numbers they are lying
i,e
51 are lie in range of 50 - 59
85 are lie in range of 80 - 89


script 1:

  1. #include <iostream>
  2. #include <ctime>

  3. using namespace std;

  4. void range_determination()
  5. {
  6.     int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
  7.     int b[15] = {0};
  8.     int c[15] = {0};

  9.     int i;

  10.     //devide all value by 10 to determine its range
  11.     for( i = 0; i < 15; i++)
  12.         b[i] = a[i] / 10;

  13.     //calculate each numbers range occurance
  14.     for( i = 0; i < 15; i++)
  15.         c[ b[i] ]++;

  16.     //for( i = 0; i < 10; i++)
  17.     //    cout << i << " = " << c[i] << endl;


  18. }

  19. int main()
  20. {
  21.     int i;

  22.     for( i = 0; i < 5000000; i++)
  23.         range_determination();

  24.     cout << "clock needed = " << clock() << endl;

  25.     return 0;
  26. }
复制代码


script 2:

  1. #include <iostream>
  2. #include <ctime>

  3. using namespace std;

  4. void range_determination()
  5. {
  6.     int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
  7.     int b[15] = {0};
  8.     int i, j;

  9.     for(i = 0; i < 15; i++)
  10.     {
  11.         if( a[i] >= 0 && a[i] <= 9 )
  12.             b[0]++;
  13.         else if ( a[i] >= 10 && a[i] <= 19 )
  14.             b[1]++;
  15.         else if ( a[i] >= 20 && a[i] <= 29 )
  16.             b[2]++;
  17.         else if ( a[i] >= 30 && a[i] <= 39 )
  18.             b[3]++;
  19.         else if ( a[i] >= 40 && a[i] <= 49 )
  20.             b[4]++;
  21.         else if ( a[i] >= 50 && a[i] <= 59 )
  22.             b[5]++;
  23.         else if ( a[i] >= 60 && a[i] <= 69 )
  24.             b[6]++;
  25.         else if ( a[i] >= 70 && a[i] <= 79 )
  26.             b[7]++;
  27.         else if ( a[i] >= 80 && a[i] <= 89 )
  28.             b[8]++;
  29.         else if ( a[i] >= 90 && a[i] <= 99 )
  30.             b[9]++;
  31.     }


  32.     //for( i = 0; i < 10; i++ )
  33.     //    cout << i << " = " << b[i] << endl;

  34. }

  35. int main()
  36. {
  37.     int i;

  38.     for( i = 0; i < 5000000; i++)
  39.         range_determination();

  40.     cout << "clock needed = " << clock() << endl;

  41.     return 0;
  42. }
复制代码


script 3:

  1. #include <iostream>
  2. #include <ctime>

  3. using namespace std;

  4. void range_determination()
  5. {
  6.     int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
  7.     int b[15] = {0};
  8.     int i, j;

  9.     for(i = 0; i < 15; i++)
  10.     {
  11.         if( a[i] >= 0 && a[i] <= 9 )
  12.             b[0]++;
  13.         if ( a[i] >= 10 && a[i] <= 19 )
  14.             b[1]++;
  15.         if ( a[i] >= 20 && a[i] <= 29 )
  16.             b[2]++;
  17.         if ( a[i] >= 30 && a[i] <= 39 )
  18.             b[3]++;
  19.         if ( a[i] >= 40 && a[i] <= 49 )
  20.             b[4]++;
  21.         if ( a[i] >= 50 && a[i] <= 59 )
  22.             b[5]++;
  23.         if ( a[i] >= 60 && a[i] <= 69 )
  24.             b[6]++;
  25.         if ( a[i] >= 70 && a[i] <= 79 )
  26.             b[7]++;
  27.         if ( a[i] >= 80 && a[i] <= 89 )
  28.             b[8]++;
  29.         if ( a[i] >= 90 && a[i] <= 99 )
  30.             b[9]++;
  31.     }


  32.     //for( i = 0; i < 10; i++ )
  33.     //    cout << i << " = " << b[i] << endl;

  34. }

  35. int main()
  36. {
  37.     int i;

  38.     for( i = 0; i < 5000000; i++)
  39.         range_determination();

  40.     cout << "clock needed = " << clock() << endl;

  41.     return 0;
  42. }
复制代码


测试结果
script 1:
yee@zyee:~/programming/performance_tune$ time  ./comparison
clock needed = 1220000

real        0m1.230s
user        0m1.224s
sys        0m0.008s
yee@zyee:~/programming/performance_tune$ time  ./comparison
clock needed = 1220000

real        0m1.229s
user        0m1.220s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison
clock needed = 1260000

real        0m1.265s
user        0m1.260s
sys        0m0.008s
yee@zyee:~/programming/performance_tune$ time  ./comparison
clock needed = 1220000

real        0m1.229s
user        0m1.228s
sys        0m0.000s
yee@zyee:~/programming/performance_tune$ time  ./comparison
clock needed = 1370000

real        0m1.431s
user        0m1.372s
sys        0m0.012s

Average Time Consume = 1.2768s


script 2:
yee@zyee:~/programming/performance_tune$ time  ./comparison2
clock needed = 1910000

real        0m1.922s
user        0m1.916s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison2
clock needed = 1910000

real        0m1.921s
user        0m1.912s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison2
clock needed = 1910000

real        0m1.920s
user        0m1.916s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison2
clock needed = 1920000

real        0m1.924s
user        0m1.920s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison2
clock needed = 1910000

real        0m1.921s
user        0m1.916s
sys        0m0.004s

Average Time Consume = 1.9216s



script 3:
yee@zyee:~/programming/performance_tune$ time  ./comparison3
clock needed = 2670000

real        0m2.680s
user        0m2.676s
sys        0m0.008s
yee@zyee:~/programming/performance_tune$ time  ./comparison3
clock needed = 2700000

real        0m2.707s
user        0m2.700s
sys        0m0.008s
yee@zyee:~/programming/performance_tune$ time  ./comparison3
clock needed = 2680000

real        0m2.683s
user        0m2.680s
sys        0m0.004s
yee@zyee:~/programming/performance_tune$ time  ./comparison3
clock needed = 2740000

real        0m2.799s
user        0m2.744s
sys        0m0.012s
yee@zyee:~/programming/performance_tune$ time  ./comparison3
clock needed = 2710000

real        0m2.716s
user        0m2.712s
sys        0m0.004s

Average Time Consume = 2.7164s


结论:
从这里看得出, if-else statement, 的确会耗去好些处理器资源。 在不正当的coding下, 浪费的资源非常可观
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 23-10-2008 05:15 PM | 显示全部楼层
保留。。。。。。。。
回复

使用道具 举报

 楼主| 发表于 23-10-2008 05:16 PM | 显示全部楼层
保留...........
回复

使用道具 举报

发表于 23-10-2008 07:10 PM | 显示全部楼层
你 用 if-else statement 的logic 和 用for loop 的 logic 不一样, 我觉得不能就此做定论。

我尝试把script 1 改了一点 :
  1. #include <iostream>
  2. #include <ctime>

  3. using namespace std;

  4. void range_determination()
  5. {
  6. //combination of 15 numbers below 100
  7.     int a[] = {15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
  8.     int b[10]={0};

  9.     int i;

  10.     //devide all value by 10 to determine its range
  11.     for( i = 0; i < 15; i++)
  12.         b[a[i] / 10]++;


  13.     //for( i = 0; i < 10; i++)
  14.         //cout << i << " = " << b[i] << endl;
  15. }

  16. int main()
  17. {
  18.     int i;

  19.     for( i = 0; i < 5000000; i++)
  20.         range_determination();

  21.     cout << "clock needed = " << clock() << endl;

  22.     return 0;
  23. }
复制代码

[ 本帖最后由 文世杰 于 23-10-2008 07:22 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 23-10-2008 08:26 PM | 显示全部楼层

回复 4# 文世杰 的帖子

logic 的确是不同, 不过在此示例中以array直接运作会来得比if-else逐个判断来得好。

这个示例的目的不是争论foor 与 if-else 的定论,而是说明在次情况下, 以array会来得更加的合适,顺便希望高手们指明哪个logic在某些情况下更为适当。

还望高手们赐教
回复

使用道具 举报

发表于 24-10-2008 10:19 AM | 显示全部楼层
在你的示例当中 script 1会比较好,明确的来讲是因为它用了算法 b[x] = a[x] / 10。
你的例子在这里证明了算法的力量。
回复

使用道具 举报

Follow Us
发表于 24-10-2008 10:31 AM | 显示全部楼层
厄~~~ 第一種寫法是正常人都會用的吧, 第二種逐一判斷當然比較耗時, 而且如果是要判斷上萬的話, 應該會增加if... else到瘋
回复

使用道具 举报

 楼主| 发表于 24-10-2008 11:01 AM | 显示全部楼层

回复 7# super-tomato 的帖子

对资深程式员来说的确是简单。 对新手却不然

之前我就走了好些的冤枉路(现在还是),这里是希望大家能够分享心得,经验, 让我们新手能够少走写冤枉路罢了。

虽然小弟的程式在高手里不足一顾,但是希望因此高手们能够分享出更好更有效率的方法
回复

使用道具 举报


ADVERTISEMENT

发表于 24-10-2008 11:13 AM | 显示全部楼层
原帖由 onlylonly 于 24-10-2008 11:01 AM 发表&#160;
对资深程式员来说的确是简单。 对新手却不然

之前我就走了好些的冤枉路(现在还是),这里是希望大家能够分享心得,经验, 让我们新手能够少走写冤枉路罢了。

虽然小弟的程式在高手里不足一顾,但是希望因此高 ...



如只是以上的coding而論, 要達到處理最快的方式就是不使用函數, 直接寫在 main 中不搜索函數的address


#include <iostream>
#include <ctime>

using namespace std;

int main()
{
&#160;&#160; &#160;int i, j , a[] = {15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};;

&#160;&#160; &#160;for( i = 0; i < 5000000; i++)

{

int b[15] = {}, c[15] = {};



for( j = 0; j < 15; j++)

b[j] = a[j] / 10;



for( j = 0; j < 15; j++)

c[ b[j] ]++;
&#160;&#160; &#160;}

&#160;&#160; &#160;cout << "clock needed = " << clock() << endl;


&#160;&#160; &#160;return 0;
}

[ 本帖最后由 super-tomato 于 24-10-2008 11:18 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 24-10-2008 11:36 AM | 显示全部楼层

回复 9# super-tomato 的帖子

原来如此, 受教了
回复

使用道具 举报

发表于 25-10-2008 11:28 PM | 显示全部楼层
除了tuning, 我认为future enhancement也很重要

if-else, 正如上面所说
如果只是几个case, 就用if-else
如果handle几万个case, if-else当然是白痴的行为

if-else
就好想一条highway, 对着路牌转弯
但如果你的case很多的话, 你的highway就非常的长
可以避免就避免把~~


future enhancement也很重要
想想, 你的program能last多久,
会不会因时间而改变
更改时的时间和金钱
等等

除此之外, OO, design pattern, reusability都很重要

我不是玉, 纯属交流
回复

使用道具 举报

发表于 26-10-2008 10:25 AM | 显示全部楼层
我觉得 code optimization 也是很重要的, 就是所用的 algorithm 要 time-efficient and memory-efficient.
回复

使用道具 举报

 楼主| 发表于 26-10-2008 11:31 AM | 显示全部楼层

回复 12# solidx 的帖子

同意, 比如用const pass by reference 来达到memory efficient, 或用 memcpy 来复制array等。
回复

使用道具 举报

发表于 28-10-2008 10:08 PM | 显示全部楼层
原帖由 onlylonly 于 26-10-2008 11:31 AM 发表
同意, 比如用const pass by reference 来达到memory efficient, 或用 memcpy 来复制array等。

用memcpy 就要小心src 跟dst 不好overlap 咯, 不然就用memmove 好了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 07:22 AM , Processed in 0.123787 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表