免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3067 | 回复: 9
打印 上一主题 下一主题

从哪些方面可以提高程序的效率 [复制链接]

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-06 08:51 |只看该作者 |倒序浏览
大家说说自己的经验,比如时间、空间、算法、语言、编译器方面。
有哪些规则可以借鉴的,希望大家可以共享。

论坛徽章:
0
2 [报告]
发表于 2011-01-06 08:56 |只看该作者
根据木桶原理 还是得先看程序性能瓶颈在哪儿 再针对性的优化短板
要说一般的 也没有特别的规则 毕竟有些时候多个因素就像是跷跷板 比如时间和空间

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2011-01-06 08:58 |只看该作者
架构、算法是决定性因素,然后再考虑语言、编译器等因素

论坛徽章:
0
4 [报告]
发表于 2011-01-06 09:16 |只看该作者
gcc编译器优化选项:
       -O
       -O1 Optimize.  Optimizing compilation takes somewhat more time, and a lot more memory for a large function.

           With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal
           of compilation time.

           -O turns on the following optimization flags:

           -fauto-inc-dec -fcprop-registers -fdce -fdefer-pop -fdelayed-branch -fdse -fguess-branch-probability -fif-conversion2
           -fif-conversion -finline-small-functions -fipa-pure-const -fipa-reference -fmerge-constants -fsplit-wide-types
           -ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-fre
           -ftree-sra -ftree-ter -funit-at-a-time

           -O also turns on -fomit-frame-pointer on machines where doing so does not interfere with debugging.

       -O2 Optimize even more.  GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.  As compared
           to -O, this option increases both compilation time and the performance of the generated code.

           -O2 turns on all optimization flags specified by -O.  It also turns on the following optimization flags: -fthread-jumps
           -falign-functions  -falign-jumps -falign-loops  -falign-labels -fcaller-saves -fcrossjumping -fcse-follow-jumps
           -fcse-skip-blocks -fdelete-null-pointer-checks -fexpensive-optimizations -fgcse  -fgcse-lm -findirect-inlining
           -foptimize-sibling-calls -fpeephole2 -fregmove -freorder-blocks  -freorder-functions -frerun-cse-after-loop -fsched-interblock
           -fsched-spec -fschedule-insns  -fschedule-insns2 -fstrict-aliasing -fstrict-overflow -ftree-switch-conversion -ftree-pre
           -ftree-vrp

           Please note the warning under -fgcse about invoking -O2 on programs that use computed gotos.

           NOTE: In Ubuntu 8.10 and later versions, -D_FORTIFY_SOURCE=2 is set by default, and is activated when -O is set to 2 or higher.
           This enables additional compile-time and run-time checks for several libc functions.  To disable, specify either
           -U_FORTIFY_SOURCE or -D_FORTIFY_SOURCE=0.

       -O3 Optimize yet more.  -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops,
           -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize options.

       -O0 Reduce compilation time and make debugging produce the expected results.  This is the default.

       -Os Optimize for size.  -Os enables all -O2 optimizations that do not typically increase code size.  It also performs further
           optimizations designed to reduce code size.

           -Os disables the following optimization flags: -falign-functions  -falign-jumps  -falign-loops -falign-labels  -freorder-blocks
           -freorder-blocks-and-partition -fprefetch-loop-arrays  -ftree-vect-loop-version

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
5 [报告]
发表于 2011-01-06 09:19 |只看该作者
我补充小点方面的,实现时多用库函数。

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
6 [报告]
发表于 2011-01-06 10:33 |只看该作者
还有就是一些编写代码时的技巧。

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
7 [报告]
发表于 2011-01-06 10:36 |只看该作者
我觉得绝大多数情况下是算法比较重要,然后是程序的设计,然后是程序的实现,最后是编译器。

当然还要看语言,比如说C要比Java快很多很多。

论坛徽章:
0
8 [报告]
发表于 2011-01-06 10:38 |只看该作者
好的设计架构是关键 另外程序本身的操作瓶颈 也很关键 其他的适当权衡...

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
9 [报告]
发表于 2011-01-06 14:35 |只看该作者
本帖最后由 liugang9931706 于 2011-01-06 17:07 编辑

把你们的答案合并起来,然后按优先级排列后制定提高效率的手段,就是问题的答案了。


1、架构(全局效率)
2、算法,用的最多,调用最多的算法,80%原则,(局部效率)
3、数据结构的设计,考虑字节对齐等(平台效率)
4、具体语言和编译器,重入性等考虑(尽量使用非static)
5、可以使用汇编实现经常调用的操作
6、使用乘法代替除法,移位代替乘法、除法等

大家还有什么可以继承补充。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2011-01-06 18:57 |只看该作者
你是指运行效率还是开发效率?

宁可浪费机器十分钟,也不愿意浪费人1分钟。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP