在32位CPU,是一个“整数”类型比“短”型更有效率? [英] On 32-bit CPUs, is an 'integer' type more efficient than a 'short' type?

查看:140
本文介绍了在32位CPU,是一个“整数”类型比“短”型更有效率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个32位CPU,一个整数是4个字节,短整数是2个字节。如果我写一个C / C ++应用程序,使用,将永远适合短整型的规定范围内的许多数值,它是更有效地使用4字节的整数或2个字节的整数?

On a 32-bit CPU, an integer is 4 bytes and a short integer is 2 bytes. If I am writing a C/C++ application that uses many numeric values that will always fit within the provided range of a short integer, is it more efficient to use 4 byte integers or 2 byte integers?

我听说它建议4字节的整数是更有效,因为这符合总线从内存到CPU的带宽。但是,如果我的CPU封装在一个平行单次(因此跨越总线的4字节的带宽)?

I have heard it suggested that 4 byte integers are more efficient as this fits the bandwidth of the bus from memory to the CPU. However, if I am adding together two short integers, would the CPU package both values in a single pass in parallel (thus spanning the 4 byte bandwidth of the bus)?

推荐答案

是的,你一定要使用32位整数一个32位的CPU上,否则最终可能会屏蔽掉未使用的位(即,它会一直做在32位的数学,然后将其转换的答案16位)

Yes, you should definitely use a 32 bit integer on a 32 bit CPU, otherwise it may end up masking off the unused bits (i.e., it will always do the maths in 32 bits, then convert the answer to 16 bits)

它不会为你做一次两个16位操作,但如果你写的code你自己和你确定它不会溢出,你可以自己做。

It won't do two 16 bit operations at once for you, but if you write the code yourself and you're sure it won't overflow, you can do it yourself.

修改:我要补充,这也一定程度上取决于你的高效的定义。虽然这将能更迅速地做32位的操作,你当然会用两倍的内存。

Edit: I should add that it also depends somewhat on your definition of "efficient". While it will be able to do 32-bit operations more quickly, you will of course use twice as much memory.

如果这些被用于中间计算在内环某处,然后使用32位。但是,如果你从磁​​盘读这篇文章,或者即使你只需要支付的高速缓存未命中,它仍然可以工作了,最好使用16位整数。如同所有的优化,有只知道一种方法:个人资料就

If these are being used for intermediate calculations in an inner loop somewhere, then use 32-bit. If, however, you're reading this from disk, or even if you just have to pay for a cache miss, it may still work out better to use 16-bit integers. As with all optimizations, there's only one way to know: profile it.

这篇关于在32位CPU,是一个“整数”类型比“短”型更有效率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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