未签名的性能VS符号整数

未签名的性能VS符号整数

本文介绍了未签名的性能VS符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何性能收益/损失通过使用无符号整数超过符号的整数?

Is there any performance gain/loss by using unsigned integers over signed integers?

如果是这样,这也适用于短期和长期呢?

If so, does this goes for short and long as well?

推荐答案

除以2的幂与 unsigned int类型,更快,因为它可以被优化成一个单一的转变指令。随着符号int ,它通常需要更多的机器指令,由于分立发的向零的,而是向右移动查房的。例如:

Division by powers of 2 is faster with unsigned int, because it can be optimized into a single shift instruction. With signed int, it usually requires more machine instructions, because division rounds towards zero, but shifting to the right rounds down. Example:

int foo(int x, unsigned y)
{
    x /= 8;
    y /= 8;
    return x + y;
}

下面是相关的 X 部分(签字师):

Here is the relevant x part (signed division):

movl 8(%ebp), %eax
leal 7(%eax), %edx
testl %eax, %eax
cmovs %edx, %eax
sarl $3, %eax

和这里是有关部分(无符号除法):

And here is the relevant y part (unsigned division):

movl 12(%ebp), %edx
shrl $3, %edx

这篇关于未签名的性能VS符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:35