本文介绍了写i!= 0会比i>快还是慢? 0为正整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个程序需要检查变量i是否大于零. i始终为正,因此说i > 0等同于说i != 0.

Imagine I have a program that needs to check if a variable i is greater than zero. i is always positive, so saying that i > 0 is equivalent to saying i != 0.

这两个表达式之间是否存在性能差异?为什么?

Is there a performance difference between those two expressions and why?

我知道并没有明显的性能差异,这更多是一个哲学问题.

I am aware that there isn't a noticable performance difference, this is more of a philosophical question.

推荐答案

我认为这没有明显的不同,但是与流行的看法相反,我将告诉您使用!=而不是><是因为前者是一个更通用的操作,如果要将代码转换为C ++并使用 iterators 而不是指针,则并非所有迭代器都支持<>(但它们全部都支持!=).

I don't think it's measurably different, but contrary to popular wisdom, I'm going to tell you to use != rather than > or < on the grounds that the former is a more general operation, and if you were going to convert your code to C++ and use iterators instead of pointers, not all iterators would support < or > (but all of them would support !=).

这篇关于写i!= 0会比i&gt;快还是慢? 0为正整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:56