Is comparing to zero faster than comparing to any other number?(与零比较是否比与任何其他数字比较快?)
问题描述
是
if(!test)
快于
if(test==-1)
我可以生产组装件,但生产的组装件太多,我永远找不到我想要的细节.我希望有人知道答案.我猜它们是相同的,除非大多数 CPU 架构都有某种与零比较"的捷径.
I can produce assembly but there is too much assembly produced and I can never locate the particulars I'm after. I was hoping someone just knows the answer. I would guess they are the same unless most CPU architectures have some sort of "compare to zero" short cut.
感谢您的帮助.
推荐答案
通常,是的.在典型的处理器中,针对零进行测试或测试符号(负/正)是简单的条件代码检查.这意味着可以重新排序指令以省略测试指令.在伪汇编中,考虑一下:
Typically, yes. In typical processors testing against zero, or testing sign (negative/positive) are simple condition code checks. This means that instructions can be re-ordered to omit a test instruction. In pseudo assembly, consider this:
Loop:
LOADCC r1, test // load test into register 1, and set condition codes
BCZS Loop // If zero was set, go to Loop
现在考虑针对 1 进行测试:
Now consider testing against 1:
Loop:
LOAD r1, test // load test into register 1
SUBT r1, 1 // Subtract Test instruction, with destination suppressed
BCNE Loop // If not equal to 1, go to Loop
现在是通常的优化前免责声明:您的程序是否太慢?不要优化,分析它.
Now for the usual pre-optimization disclaimer: Is your program too slow? Don't optimize, profile it.
这篇关于与零比较是否比与任何其他数字比较快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:与零比较是否比与任何其他数字比较快?
基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01