|
查看: 1154|回复: 13
|
抛砖引玉之 performance tune
[复制链接]
|
|
|
小弟不才, 最近忽发心思, 写了几个关于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:
- #include <iostream>
- #include <ctime>
- using namespace std;
- void range_determination()
- {
- int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
- int b[15] = {0};
- int c[15] = {0};
- int i;
- //devide all value by 10 to determine its range
- for( i = 0; i < 15; i++)
- b[i] = a[i] / 10;
- //calculate each numbers range occurance
- for( i = 0; i < 15; i++)
- c[ b[i] ]++;
- //for( i = 0; i < 10; i++)
- // cout << i << " = " << c[i] << endl;
- }
- int main()
- {
- int i;
- for( i = 0; i < 5000000; i++)
- range_determination();
- cout << "clock needed = " << clock() << endl;
- return 0;
- }
复制代码
script 2:
- #include <iostream>
- #include <ctime>
- using namespace std;
- void range_determination()
- {
- int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
- int b[15] = {0};
- int i, j;
- for(i = 0; i < 15; i++)
- {
- if( a[i] >= 0 && a[i] <= 9 )
- b[0]++;
- else if ( a[i] >= 10 && a[i] <= 19 )
- b[1]++;
- else if ( a[i] >= 20 && a[i] <= 29 )
- b[2]++;
- else if ( a[i] >= 30 && a[i] <= 39 )
- b[3]++;
- else if ( a[i] >= 40 && a[i] <= 49 )
- b[4]++;
- else if ( a[i] >= 50 && a[i] <= 59 )
- b[5]++;
- else if ( a[i] >= 60 && a[i] <= 69 )
- b[6]++;
- else if ( a[i] >= 70 && a[i] <= 79 )
- b[7]++;
- else if ( a[i] >= 80 && a[i] <= 89 )
- b[8]++;
- else if ( a[i] >= 90 && a[i] <= 99 )
- b[9]++;
- }
- //for( i = 0; i < 10; i++ )
- // cout << i << " = " << b[i] << endl;
- }
- int main()
- {
- int i;
- for( i = 0; i < 5000000; i++)
- range_determination();
- cout << "clock needed = " << clock() << endl;
- return 0;
- }
复制代码
script 3:
- #include <iostream>
- #include <ctime>
- using namespace std;
- void range_determination()
- {
- int a[] = { 15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
- int b[15] = {0};
- int i, j;
- for(i = 0; i < 15; i++)
- {
- if( a[i] >= 0 && a[i] <= 9 )
- b[0]++;
- if ( a[i] >= 10 && a[i] <= 19 )
- b[1]++;
- if ( a[i] >= 20 && a[i] <= 29 )
- b[2]++;
- if ( a[i] >= 30 && a[i] <= 39 )
- b[3]++;
- if ( a[i] >= 40 && a[i] <= 49 )
- b[4]++;
- if ( a[i] >= 50 && a[i] <= 59 )
- b[5]++;
- if ( a[i] >= 60 && a[i] <= 69 )
- b[6]++;
- if ( a[i] >= 70 && a[i] <= 79 )
- b[7]++;
- if ( a[i] >= 80 && a[i] <= 89 )
- b[8]++;
- if ( a[i] >= 90 && a[i] <= 99 )
- b[9]++;
- }
- //for( i = 0; i < 10; i++ )
- // cout << i << " = " << b[i] << endl;
- }
- int main()
- {
- int i;
- for( i = 0; i < 5000000; i++)
- range_determination();
- cout << "clock needed = " << clock() << endl;
- return 0;
- }
复制代码
测试结果
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下, 浪费的资源非常可观 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 改了一点 :- #include <iostream>
- #include <ctime>
- using namespace std;
- void range_determination()
- {
- //combination of 15 numbers below 100
- int a[] = {15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};
- int b[10]={0};
- int i;
- //devide all value by 10 to determine its range
- for( i = 0; i < 15; i++)
- b[a[i] / 10]++;
- //for( i = 0; i < 10; i++)
- //cout << i << " = " << b[i] << endl;
- }
- int main()
- {
- int i;
- for( i = 0; i < 5000000; i++)
- range_determination();
- cout << "clock needed = " << clock() << endl;
- return 0;
- }
复制代码
[ 本帖最后由 文世杰 于 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。
你的例子在这里证明了算法的力量。 |
|
|
|
|
|
|
|
|
|
|
发表于 24-10-2008 10:31 AM
|
显示全部楼层
厄~~~ 第一種寫法是正常人都會用的吧, 第二種逐一判斷當然比較耗時, 而且如果是要判斷上萬的話, 應該會增加if... else到瘋 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-10-2008 11:01 AM
|
显示全部楼层
回复 7# super-tomato 的帖子
对资深程式员来说的确是简单。 对新手却不然
之前我就走了好些的冤枉路(现在还是),这里是希望大家能够分享心得,经验, 让我们新手能够少走写冤枉路罢了。
虽然小弟的程式在高手里不足一顾,但是希望因此高手们能够分享出更好更有效率的方法 |
|
|
|
|
|
|
|
|
|
|
发表于 24-10-2008 11:13 AM
|
显示全部楼层
原帖由 onlylonly 于 24-10-2008 11:01 AM 发表 
对资深程式员来说的确是简单。 对新手却不然
之前我就走了好些的冤枉路(现在还是),这里是希望大家能够分享心得,经验, 让我们新手能够少走写冤枉路罢了。
虽然小弟的程式在高手里不足一顾,但是希望因此高 ...
如只是以上的coding而論, 要達到處理最快的方式就是不使用函數, 直接寫在 main 中不搜索函數的address
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    int i, j , a[] = {15,21,48,87,54,13,56,98,45,12,54,87,56,32,87};;
    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] ]++;
    }
    cout << "clock needed = " << clock() << endl;
    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 好了。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|