[英]Is It More Efficient to Do a Less Than comparison OR a Less Than Or Equal To?
I'm wondering if it's more efficient to do a less than or equal to comparison in a loop or a less than comparison. 我想知道在循环中进行小于或等于比较的效率是否更高,或者是否比对比小。 Does the
<=
operator instruct the computer to make two comparisons (is it less than, is it equal to), or does it simplify it? <=
运算符是否指示计算机进行两次比较(是否小于,是否相等),还是简化它? Take the following example. 以下面的例子为例。 I want a loop than increments to 1000. Should I set the ceiling to 1001 and tell it that
while i is < (OR !=) 1001, i++;
我想要一个循环而不是增量到1000.我应该将上限设置为1001并告诉它,
while i is < (OR !=) 1001, i++;
? ? Or should I tell it that
while i <= 1000, i++;
或者我应该告诉它,
while i <= 1000, i++;
? ? Will the compiler (GCC) simplify it to the same basic instructions?
编译器(GCC)会将其简化为相同的基本指令吗?
The machine level architecture will have OP codes for both < and <= operations and both comparisons can be made in one cycle of the CPU. 机器级架构将具有用于<和<=操作的OP代码,并且两个比较都可以在CPU的一个周期中进行。 Meaning it makes no difference.
意思是没有区别。
It depends on the architecture. 这取决于架构。
The original von Neumann IAS architecture (1945) did have only >=
comparison. 最初的冯诺依曼IAS架构(1945)确实只有
>=
比较。
Intel 8086 can use Loop label
paradigm, which corresponds to do { } while (--cx > 0);
英特尔8086可以使用
Loop label
范例,它对应于do { } while (--cx > 0);
In legacy architectures, LOOP
was not only smaller, but faster. 在传统架构中,
LOOP
不仅更小,而且速度更快。 In modern architectures LOOP
is considered complex operation, which is slower than dec ecx; jnz label;
在现代架构中,
LOOP
被认为是复杂的操作,它比dec ecx; jnz label;
慢dec ecx; jnz label;
dec ecx; jnz label;
When optimizing for size (-Os) this can still have significance. 在优化尺寸(-Os)时,这仍然具有重要意义。
Further considerations are that some (RISC) architectures do not have explicit flag registers. 进一步的考虑是一些(RISC)架构没有显式标志寄存器。 Then comparison can't be given free, as a side effect of loop decrement.
然后比较不能自由,作为循环减量的副作用。 Some RISC architectures have also a special 'zero' register, which means, that comparison (and every other mathematical operations) with zero is always available.
一些RISC架构还有一个特殊的“零”寄存器,这意味着,零比较(以及所有其他数学运算)始终可用。 RISCs with jump delay slots may even benefit from using post decrement:
do { } while (a-- > 0);
具有跳转延迟时隙的 RISC甚至可以通过使用后递减来获益:
do { } while (a-- > 0);
An optimizing compiler should be able to convert a simple loop regardless of the syntax to the most optimized version for the given architecture. 优化编译器应该能够将无论语法的简单循环转换为给定体系结构的最优化版本。 A complex loop would have a dependency to the iterator, side effects, or both:
for (i=0;i<5;i++) func(i);
复杂循环将依赖于迭代器,副作用或两者:
for (i=0;i<5;i++) func(i);
. 。
Measure it. 测量它。 Only then you can be absolutely sure which is faster.
只有这样你才能绝对确定哪个更快。
You may think a lot about all the parts that play a role here (compiler, optimisation, processor, etc.). 您可能会想到很多关于在这里发挥作用的所有部分(编译器,优化,处理器等)。 But in the end it is faster if it takes less time.
但最终如果花费的时间更少则会更快。 It's as simple as that.
就这么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.