[英]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. 所以正在搞乱Prime数字生成器功能,并提出了一个奇怪的行为,其中“!=”人们建议最有效率实际上效率最低,“<=”这是最差的最佳选择。
C# 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;
}
Output: 输出:
<= 1319991
!= 1321251
Similarly In C++(On a different machine) 同样在C ++中(在不同的机器上)
include <cstddef>
#include <limits>
int main() {
for(size_t i(0) ; i <= 10000000000 ; ++i);
}
Output: 输出:
<=
real 0m16.538s
user 0m16.460s
sys 0m0.000s
~ [master] $ vim d.cc
!=
real 0m16.860s
user 0m16.780s
sys 0m0.000s
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? 是否有任何优化<=
哪个不适用于!=
或者是一些奇怪的cpu行为?
It doesn't make sense that there would be a difference, assuming the result is the same number of iterations. 假设结果是相同的迭代次数,那就没有意义。
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]). 如果我们假设它是一个x86处理器,那么!=
变成jne
(或者je
,取决于“它是”或“它不是”跳转[1]的哪一侧)。 A <=
will do jle
or jgt
depending on which way the loop goes. A <=
将执行jle
或jgt
具体取决于循环的方式。 Whilst the instructions are different, other processors have the same sort of instructions. 虽然说明不同,但其他处理器具有相同的指令。
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. 16s中差异小于0.2秒并不是一个巨大的差异,你可能只是有一些网络数据包,硬盘中断或一些后台运行的时间。
[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. [1]例如,具有固定迭代集的for
循环通常只有“if not true,跳转到循环开始”,同样适用于while
循环。
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);
}
}
Compiled with g++ -O3 primes.cpp
. 用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. !=
的最快时间是3.326s, <=
3.329,最慢的!=
是3.332,而<=
它是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. 之前在我的机器上运行了很多基准测试,我知道在毫秒数字中没有任何意义,所以我认为两者都需要3.33秒。
And just to confirm: 并且只是为了确认:
--- 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
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
. 所述“不等于”和“小于或等于”之间的整个差别是jne
VS jle
指令-这是从输出汇编g++
代码的两种变体的-这是从输出整个diff
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.