比较运算符的性能< =与!= [英] Comparison operator performance <= against !=
问题描述
首先要说明代码的可读性优于微优化,我们宁可将其留给编译器.这只是一个奇怪的案例,其中具体细节似乎不符合一般建议
Lets start out with stating code-readability beats micro-optimizations and we should rather leave that to the compiler. This was just a weird case where the specifics seemed interesting against general recommendation
因此,我很想知道素数生成器函数,并提出了一个怪异的行为,其中!="被推荐为最有效的实际上实际上是效率最低的,而< =则被认为是最差的".最佳选择.
So was messing about with a Prime number generator function, and came up with a weird behavior where "!=" which people recommend to be the most efficient actually the least efficient and "<=" which is the worst as the best option.
C#
private static void Main(string[] args) {
long totalTicks = 0;
for (int i = 0; i < 100; ++i) {
var stopWatch = Stopwatch.StartNew();
PrintPrimes(15000);
totalTicks += stopWatch.ElapsedTicks;
}
Console.WriteLine("\n\n\n\nTick Average: {0}", totalTicks / 100);
Console.Read();
}
private static void PrintPrimes(int numberRequired) {
if (numberRequired < 1)
return;
Console.Write("{0}\t", 2);
int primeTest = 3;
/****** UPDATE NEXT TWO LINES TO TEST FOR != *****/
int numPrimes = 2; // set numPrimes = 1 for !=
while (numPrimes <= numberRequired) { // switch <= to !=
if (IsPrime(primeTest)) {
Console.Write("{0}\t", primeTest);
++numPrimes;
}
primeTest += 2;
}
}
private static bool IsPrime(int test) {
for (int i = 3; i * i <= test; i = 2 + i)
if (test % i == 0)
return false;
return true;
}
输出:
<= 1319991
!= 1321251
类似地在C ++中(在另一台计算机上)
include <cstddef>
#include <limits>
int main() {
for(size_t i(0) ; i <= 10000000000 ; ++i);
}
输出:
<=
real 0m16.538s
user 0m16.460s
sys 0m0.000s
~ [master] $ vim d.cc
!=
real 0m16.860s
user 0m16.780s
sys 0m0.000s
循环运行相同的时间. <=
是否有不适用于!=
的优化,还是某些奇怪的cpu行为?
The loops run the same amount of times. Are there any optimizations for <=
which does not apply for !=
or is it some weird cpu behavior?
推荐答案
假设结果是相同的迭代次数,则没有区别是没有道理的.
It doesn't make sense that there would be a difference, assuming the result is the same number of iterations.
如果我们假设它是x86处理器,则!=
变为jne
(或je
,具体取决于是"或不是"的哪一侧跳转[1]). <=
将执行jle
或jgt
,具体取决于循环进行的方式.虽然指令不同,但其他处理器具有相同种类的指令.
If we assume it's an x86 processor, !=
turns into jne
(or je
, depending on which side of the "it is" or "it is not" jumps [1]). A <=
will do jle
or jgt
depending on which way the loop goes. Whilst the instructions are different, other processors have the same sort of instructions.
我怀疑您有测量错误. 16秒之内的相差不到0.2秒并不是什么大的差别,那时候您可能只是有更多的网络数据包,硬盘中断或一些后台进程在运行.
I suspect you have measurement errors. A difference of less than 0.2 seconds out of 16s is not a huge difference, and you may simply have had a few more network packets, hard disk interrupts or some background process running that time.
[1]例如,具有固定迭代集的for
循环通常仅具有如果不是true,则跳转到循环的开始",并且对while
循环也是如此.
[1] A for
loop that has a fixed set of iterations, for example, will typically just have a "if not true, jump to beginning of loop", and the same applies to while
loops.
我只是在我的机器上运行了
I just ran this on my machine:
bool IsPrime(int test) {
for (int i = 3; i * i <= test; i = 2 + i)
if (test % i == 0)
return false;
return true;
}
void PrintPrimes(int numberRequired) {
if (numberRequired < 1)
return;
int primeTest = 3;
/****** UPDATE NEXT TWO LINES TO TEST FOR != *****/
int numPrimes = 2; // set numPrimes = 1 for !=
while (numPrimes != numberRequired) { // switch <= to !=
if (IsPrime(primeTest)) {
++numPrimes;
}
primeTest += 2;
}
}
int main()
{
long totalTicks = 0;
for (int i = 0; i < 100; ++i) {
PrintPrimes(15000);
}
}
与g++ -O3 primes.cpp
一起编译.在主循环中使用!=
和<=
之间的区别并不明显. !=
最快的时间是3.326s,<=
3.329的最快时间是!=
的最慢时间是3.332,而<=
的最慢时间是3.335s.之前在我的计算机上运行过许多基准测试后,我知道毫秒数没有意义,因此我要说两者都花费了3.33秒.
Compiled with g++ -O3 primes.cpp
. The difference between using !=
and <=
in the main loop is not noticeable. The fastest time for !=
is 3.326s, for <=
3.329, the slowest for !=
is 3.332 and with <=
it is 3.335s. Having run many benchmarks on my machine before, I know that there is no significance in the millisecond digit, so I would say that it takes 3.33 seconds for both.
只是为了确认:
--- primesne.s 2013-04-30 23:52:10.840513380 +0100
+++ primesle.s 2013-04-30 23:52:35.457639603 +0100
@@ -46,7 +46,7 @@
.L3:
addl $2, %esi
cmpl $15000, %edi
- jne .L10
+ jle .L10
subl $1, %r9d
jne .L2
xorl %eax, %eax
不等于"和小于或等于"之间的全部区别是jne
vs jle
指令-这是代码的两个变体的g++
的汇编程序输出-即从diff
整个输出.
The entire difference between the "not equal" and "less or equal" is the jne
vs jle
instructions - this is the assembler output from g++
of the two variants of the code - and that is the ENTIRE output from diff
.
这篇关于比较运算符的性能< =与!=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!