使用整数作为布尔值更快吗?
从内存访问的角度来看......是否值得尝试这样的优化?
int boolean_value = 0;
//magical code happens and boolean_value could be 0 or 1
if(boolean_value)
{
//do something
}
当然,与
unsigned char boolean_value = 0;
//magical code happens and boolean_value could be 0 or 1
if(boolean_value)
{
//do something
}
整数 4 相比,unsigned char 只占用 1 个字节(假设此处为 32 位平台),但我的理解是,处理器从内存中读取整数值会更快。
From a memory access standpoint... is it worth attempting an optimization like this?
int boolean_value = 0;
//magical code happens and boolean_value could be 0 or 1
if(boolean_value)
{
//do something
}
Instead of
unsigned char boolean_value = 0;
//magical code happens and boolean_value could be 0 or 1
if(boolean_value)
{
//do something
}
The unsigned char of course takes up only 1 byte as apposed to the integers 4 (assuming 32 bit platform here), but my understanding is that it would be faster for a processor to read the integer value from memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它可能会更快,也可能不会更快,并且速度取决于很多因素,因此不可能给出通用答案。例如:硬件架构、编译器、编译器选项、数据量(是否适合 L1 缓存?)、与 CPU 竞争的其他因素等。
因此,正确的答案是:尝试两种方法并针对您的特定情况进行衡量。
如果测量结果没有表明一种方法明显快于另一种方法,请选择更清晰的方法。
It may or may not be faster, and the speed depends on so many things that a generic answer is impossible. For example: hardware architecture, compiler, compiler options, amount of data (does it fit into L1 cache?), other things competing for the CPU, etc.
The correct answer, therefore, is: try both ways and measure for your particular case.
If measurement does not indicate that one method is significantly faster than the other, opt for the one that is clearer.
可能不会。在几乎所有现代处理器中,都会根据处理器的字大小来获取内存。在您的情况下,即使要取出一个字节的内存,您的处理器也可能会根据该处理器的缓存来获取整个 32 位字或更多字。您的架构可能会有所不同,因此您需要了解 CPU 的工作原理。
但正如其他人所说,尝试并衡量它并没有什么坏处。
Probably not. In almost all modern processors, memory will get fetched based on the word size of the processor. In your case, even to get one byte of memory out, your processor probably fetches the entire 32-bit word or more based on the caching of that processor. Your architecture may vary, so you will want to understand how your CPU works to gauge.
But as others have said, it doesn't hurt to try it and measure it.
这几乎从来都不是一个好主意。许多系统只能一次从内存中读取字大小的块,因此读取一个字节然后进行掩码或移位实际上会占用更多代码空间和同样多的(数据)内存。如果您使用的是一个不起眼的微型系统,请进行测量,但一般来说,这实际上会减慢并使您的代码变得臃肿。
This is almost never a good idea. Many systems can only read word-sized chunks from memory at once, so reading a byte then masking or shifting will actually take more code space and just as much (data) memory. If you're using an obscure tiny system, measure, but in general this will actually slow down and bloat your code.
询问
unsigned char
与int
占用多少内存仅当它位于数组中时才有意义(或者可能是结构体,如果您小心的话)对元素进行排序以进行对齐)。作为一个单独的变量,您根本不可能节省任何内存,并且编译器可能会生成更大的代码来截断寄存器的高位。作为一般策略,除非在数组中,否则永远不要使用小于
int
的类型,除非您有一个非常好的理由除了试图节省空间。Asking how much memory
unsigned char
takes versusint
is only meaningful when it's in an array (or possibly a structure, if you're careful to order the elements to take care of alignment). As a lone variable, it's very unlikely that you save any memory at all, and the compiler is likely to generate larger code to truncate the upper bits of registers.As a general policy, never use smaller-than-
int
types except in arrays unless you have a really good reason other than trying to save space.遵循优化的标准规则。首先,不要优化。然后测试您的代码在某个时候是否需要它。然后优化那个点。此链接提供了优化主题的精彩介绍。
Follow the standard rules of optimization. First, don't optimize. Then test if your code needs it at some point. Then optimize that point. This link provides an excellent intro to the topic of optimization.