这是更快:X&所述;&。1或X;小于10? [英] Which is faster: x<<1 or x<<10?

查看:136
本文介绍了这是更快:X&所述;&。1或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屋!

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