这是更快:X&所述;&。1或X;小于10? [英] Which is faster: x<<1 or x<<10?
问题描述
我不希望优化什么,我发誓,我只想问这个问题是出于好奇。
我知道,在大多数硬件上有位移(如 SHL
, SHR
)的集合命令,这是一个单一的命令。但它的问题(纳秒的角度来看,还是CPU-圆通明智的),你有多少位转移。换句话说,要么是下列任何CPU上更快吗?
I don't want to optimize anything, I swear, I just want to ask this question out of curiosity.
I know that on most hardware there's an assembly command of bit-shift (e.g. shl
, shr
), which is a single command. But does it matter (nanosecond-wise, or CPU-tact-wise) how many bits you shift. In other words, is either of the following faster on any CPU?
x << 1;
和
x << 10;
和请不要恨我这个问题。 :)
And please don't hate me for this question. :)
推荐答案
潜在取决于CPU。
然而,所有现代的CPU(X86,ARM)使用一个桶式移位器 - 一个硬件模块专为在固定时间内执行任意移
However, all modern CPUs (x86, ARM) use a "barrel shifter" -- a hardware module specifically designed to perform arbitrary shifts in constant time.
因此,底线是...没有。没有什么区别。
So the bottom line is... no. No difference.
这篇关于这是更快:X&所述;&。1或X;小于10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!