哪个性能更好:<= 0 或 <1? [英] Which is more performant: &lt;= 0 or &lt;1?

查看:56
本文介绍了哪个性能更好:<= 0 或 <1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我学习 C 和汇编的那一天,我们被教导最好使用简单的比较来提高速度.例如,如果您说:

Back in the day when I was learning C and assembly we were taught it is better to use simple comparisons to increase speed. So for example if you say:

if(x <= 0)

对比

if(x < 1)

哪个执行得更快?我的论点(可能是错误的)是第二个几乎总是执行得更快,因为只有一个比较)即它是否小于一个,是或否.

which would execute faster? My argument (which may be wrong) is the second would almost always execute faster because there is only a single comparison) i.e. is it less than one, yes or no.

如果数字小于 0,第一个将执行得很快,因为这等同于 true,因此无需检查等于使其与第二个一样快,但是,如果数字为 0 或更多是因为它必须进行第二次比较以查看它是否等于 0.

Whereas the first will execute fast if the number is less than 0 because this equates to true there is no need to check the equals making it as fast as the second, however, it will always be slower if the number is 0 or more because it has to then do a second comparison to see if it is equal to 0.

我现在正在使用 C#,虽然为桌面开发速度不是问题(至少不是他的观点值得争论的程度),但我仍然认为需要考虑这些论点,因为我也在为移动设备开发其功能远不如台式机,而且速度确实成为此类设备的一个问题.

I am now using C# and while developing for desktops speed is not an issue (at least not to the degree that his point is worth arguing), I still think such arguments need to be considered as I am also developing for mobile devices which are much less powerful than desktops and speed does become an issue on such devices.

为了进一步考虑,我说的是整数(没有小数)和不能有负数的数字,例如 -1 或 -12,345 等(除非有错误),例如, 在处理列表或数组时,您不能有负数的项目但您想检查列表是否为空(或者如果有问题,请将 x 的值设置为负数以指示错误,例如是列表中的某些项目,但由于某种原因您无法检索整个列表,为了表明这一点,您将数字设置为负数,这与表示没有项目不同).

For further consideration, I am talking about whole numbers (no decimals) and numbers where there cannot be a negative number like -1 or -12,345 etc (unless there is an error), for example, when dealing with lists or arrays when you cant have a negative number of items but you want to check if a list is empty (or if there is a problem, set the value of x to negative to indicate error, an example is where there are some items in a list, but you cannot retrieve the whole list for some reason and to indicate this you set the number to negative which would not be the same as saying there are no items).

出于上述原因,我故意省略了显而易见的

For the reason above I deliberately left out the obvious

if(x == 0)

if(x.isnullorempty())

以及其他用于检测没有项目的列表的项目.

and other such items for detecting a list with no items.

再次考虑,我们正在讨论从数据库中检索项目的可能性,可能使用具有上述功能的 SQL 存储过程(即标准(至少在这家公司中)是返回一个负数来表示一个问题).

Again, for consideration, we are talking about the possibility of retrieving items from a database perhaps using SQL stored procedures which have the functionality mentioned (ie the standard (at least in this company) is to return a negative number to indicate a problem).

那么在这种情况下,使用上面的第一项还是第二项更好?

So in such cases, is it better to use the first or the second item above?

推荐答案

它们是相同的.两者都不比另一个快.他们都问完全相同的问题,假设 x 是一个整数.C# 不是汇编.您要求编译器生成最佳代码以获得您所要求的效果.您没有指定它如何获得该结果.

They're identical. Neither is faster than the other. They both ask precisely the same question, assuming x is an integer. C# is not assembly. You're asking the compiler to generate the best code to get the effect you are asking for. You aren't specifying how it gets that result.

另见这个答案.

我的论点(可能是错误的)是第二个几乎总是执行得更快,因为只有一个比较)即它小于一个,是或否.

My argument (which may be wrong) is the second would almost always execute faster because there is only a single comparison) i.e. is it less than one, yes or no.

显然这是错误的.如果你认为这是真的,看看会发生什么:

Clearly that's wrong. Watch what happens if you assume that's true:

<<= 更快,因为它问的问题更少.(你的论点.)

< is faster than <= because it asks fewer questions. (Your argument.)

><= 的速度相同,因为它提出了相同的问题,只是答案相反.

> is the same speed as <= because it asks the same question, just with an inverted answer.

因此<>快!但同样的论点表明 >< 快.

Thus < is faster than >! But this same argument shows > is faster than <.

只是一个反向答案"似乎潜入了一个额外的布尔运算,所以我不确定我是否遵循这个答案.

"just with an inverted answer" seems to sneak in an additional boolean operation so I'm not sure I follow this answer.

出于同样的原因,这是错误的(对于硅,有时对于软件是正确的).考虑:

That's wrong (for silicon, it is sometimes correct for software) for the same reason. Consider:

3 != 43 == 4 的计算成本更高,因为它是 3 != 4 的倒置答案,额外的布尔运算.

3 != 4 is more expensive to compute than 3 == 4, because it's 3 != 4 with an inverted answer, an additional boolean operation.

3 == 43 != 4 更昂贵,因为它是带有反向答案的 3 != 4,一个额外的布尔运算.

3 == 4 is more expensive than 3 != 4, because it's 3 != 4 with an inverted answer, an additional boolean operation.

因此,3 != 4 比它本身更昂贵.

Thus, 3 != 4 is more expensive than itself.

反向答案只是相反的问题,而不是额外的布尔运算.或者,更准确地说,它是比较结果到最终答案的不同映射.3 == 43 != 4 都要求你比较 3 和 4.这种比较会导致以太相等"或不相等".这些问题只是将平等"和不平等"映射到真"和假"的不同.两种映射都不比另一种更昂贵.

An inverted answer is just the opposite question, not an additional boolean operation. Or, to be a bit more precise, it's with a different mapping of comparison results to final answer. Both 3 == 4 and 3 != 4 require you to compare 3 and 4. That comparison results in ether "equal" or "unequal". The questions just map "equal" and "unequal" to "true" and "false" differently. Neither mapping is more expensive than the other.

这篇关于哪个性能更好:<= 0 或 <1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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