Efficient C Tips #6 – Don’t use the ternary operator
来源:互联网 发布:python数据采集 pdf 编辑:程序博客网 时间:2024/05/18 12:03
by Nigel Jones
原文: http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/
I have to confess that I like the ternary operator. K&R obviously liked it, as it is heavily featured in their seminal work. However after running experiments on a wide range of compilers I have concluded that with the optimizer turned on, you are better off with a simple if-else statement. Thus next time you write something like this:
y = (a > b) ? c : d;
be aware that as inelegant as it is in comparison, this will usually compile to better code:
if (a > b){ y = c;}else{ y = d;}
I find this frustrating, as I’ve consumed 8 lines doing what is more easily and elegantly performed in 1 line.
I can’t say that I have any particular insight as to why the ternary operator performs so poorly. Perhaps if there is a compiler writer out there, they could throw some light on the matter?
// 评论很精彩. 绝对能学到更多.
- Efficient C Tips #6 – Don’t use the ternary operator
- Efficient C Tips #8 – Use const
- Efficient C Tips #4 – Use Speed Optimization
- Efficient C Tips #9 – Use lookup tables
- Efficient C Tips #10 – Use unsigned integers
- Efficient C Tips #1 – Choosing the correct integer size
- Efficient C Tips #2 – Using the optimizer
- Why ternary operator more efficient than if-else
- Efficient C Tips #7 – Fast loops
- Don't use XP!
- Don't Use Select *
- Don't use extjs
- Don't use MongoDB
- Don't Use XP (转)
- Don't in the
- Efficient C Tips #3 – Avoiding post increment / decrement
- Efficient C Tips #5 – Make ‘local’ functions ‘static’
- Don’t Use the Win32 API PostThreadMessage() to Post Messages to UI Threads(翻译)
- Quick Linux Server Installation
- 第12周实验报告2
- pthread_cond_timedwait实例代码
- c++ unio 学习总计
- 指针的赋值和内存访问
- Efficient C Tips #6 – Don’t use the ternary operator
- 素数筛选与快速幂取模
- 一道中兴笔试题
- “NETDEV WATCHDOG: eth0: transmit timed out” 后网卡当掉
- FIREFOX与IE兼容性探讨
- 敌国布阵(线段树)
- java内存管理的学习笔记
- mysql 查询替换
- Tempter of the Bone 奇偶剪枝(深搜)