大于和大于或等于之间是否存在性能差异? [英] Is there any performance difference between greater than and greater than or equal?

查看:410
本文介绍了大于和大于或等于之间是否存在性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在当今的现代处理器上,分支条件的大于和大于或等于比较之间是否存在性能差异?如果我有一个容易发生的情况,选择>而不是>=有什么小优势吗?反之亦然吗? (这适用于Intel或AMD硬件上的编译语言)

On today's modern processors, is there any performance difference between greater than and greater than or equal comparison for a branch condition? If I have a condition that could just as easily be either, is there any slight advantage to choosing > over >= or vice-versa? (This would be for a compiled language on Intel or AMD hardware)

推荐答案

由于比较谓词的计算方式,比较不同谓词之间不应有任何明显的区别(请注意,我没有详细阅读x86手册)因此可能会有所不同):

There shouldn't be any noticeable difference between comparing different predicates, because of the way they're computed (beware I haven't read the x86 manuals in detail so it may work different):

大多数指令会产生几个标志作为副产品,通常至少具有:进位(c),溢出(o),零(z)和负数(n).

Most instructions produce several flags as a byproduct, usually you have at least: carry (c), overflow (o), zero (z) and negative (n).

使用由x-y指令创建的谓词(可靠地创建上述4个谓词),我们可以轻松地轻松算出所有所需的比较.对于无符号数字:

Using those predicates that are created by a x-y instruction (that creates the above 4 reliably) we can easily figure out all wanted comparisions trivially. For unsigned numbers:

x = y    z
x != y   !z
x < y    !c
x <= y   !c + z
x > y    c . !z
x >= y   c

因此几乎没有什么区别.但是然后存在一些差异,这主要归结为以下事实:如果我们可以使用TEST(这是AND,而不是完全的减法)或必须使用CMP(即减法). TEST的限制更大,但速度更快(通常).

So it hardly makes any difference. But then there are some differences, which mostly come down to the fact if we can use TEST (which is an AND instead of a full blown subtraction) or have to use CMP (that's the subtraction). TEST is more limited but faster (usually).

现代架构(从intel端的c2d开始)有时可以将两个µop融合到一个宏op中-所谓的macro-op融合具有一些不错的优点.而且该规则从一种架构更改为另一种架构,并且需要更长的时间.例如,仅测试溢出,奇偶校验或符号标志的分支(JO,JNO,JP,JNP,JS,JNS)可以与TEST融合,而不能与c2d和nehalems上的CMP融合(

Also modern architectures (starting from c2d on intel side) can sometimes fuse two µops into one macro op - so called macro-op fusion which has some nice advantages. And the rules for that change from one architecture to the next and are a bit longer. For example branches that test the overflow, parity or sign flag only (JO, JNO, JP, JNP, JS, JNS) can fuse with TEST but not with CMP on c2d and nehalems (you bet I looked that one up - section 7.5).

所以我们可以说这很复杂而不用担心这样的事情吗?除非您正在为编译器编写优化器,否则是因为–与您在源代码中编写的内容无关,编译器将以任何方式执行它想要的操作–并且有充分的理由(即,如果从理论上讲,JGE会更快)通常写if(x

So can we just say it's complicated and not worry about such things? That is except if you're writing an optimizer for a compiler, because really - independent of WHAT you write in your source code the compiler will do what it wants anyhow - and for good reason (ie if JGE were theoretically faster you'd have to write if (x < y) usually..). And if you really need one advice: Comparing against 0 is often faster.

这篇关于大于和大于或等于之间是否存在性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆