似乎(!$ a =='hello')总是快于($ a!='hello')
// (!$a == 'hello')
Used time: 52.743232011795
Used time: 52.633831977844
Used time: 51.452646970749
//($a != 'hello')
Used time: 76.290767908096
Used time: 81.887389183044
Used time: 64.569777011871
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?据我所知,在大多数情况下,这种优化水平是无关紧要的.问题完全出于好奇.(参考:http://www.php.net/manual/en/language.operators.comparison.php#99216)
我有一个关于创建最佳C++程序的问题.
我有一个函数来计算如下表达式:
c= a/2
c = (a*b)/2
c = (a/2) + b
Run Code Online (Sandbox Code Playgroud)
等.使用变量来存储这些值还是只使用return <expression>?
我理解创建变量会占用空间并return <expression>避免这种情况.但如果这些是多次返回,它是否会产生比声明变量更多的开销?
在 C 中,索引数组是否比?:运算符更快?
例如,会(const int[]){8, 14}[N > 10]比N > 10? 14 : 8?
c arrays performance conditional-operator micro-optimization
这实际上只是一个学术问题,我只是想知道哪一个更快.我猜这个差别可以忽略不计,但我仍然想知道.
if( (x == 1) || (x == 2) )
Run Code Online (Sandbox Code Playgroud)
VS
if (x < 3)
Run Code Online (Sandbox Code Playgroud)
谢谢!
哪个代码更好或优化或有效?
double a;
double b;
if (a == b)
return true;
Run Code Online (Sandbox Code Playgroud)
要么
if (a - b == 0)
return true;
Run Code Online (Sandbox Code Playgroud)