Should I use the smallest type possible?
A long time ago I remember reading that you should always use the smallest possible type to store your data, but nearly 开发者_JS百科every piece of code I read doesn't do this. They often use 32 bit integers all over the place.
I've heard the rationale that a 32 bit value is fetched as fast as an 8 bit value, but processors have some way of fetching several smaller values at once.. Right?
So if I use 4 bytes instead of 4 integers, shouldn't the compiler be able to optimize this so the 4 bytes is fetched/stored in a single 32 bit register?
Or is all of this really just premature optimization and the potential performance gain is negligible?
Premature optimization indeed! However, once you are optimising, it also depends on your architecture. For example, on ARM, memory accesses must be 32-bit aligned (some instructions can do this, but they just do a 32-bit access then mask/shift behind the scenes). If you use a byte, a compiler will often give each 'byte' four actual bytes of RAM so it can be accessed faster (not to mention the CPU will freak outif you try to access unaligned bytes without special code to handle them).
There is an argument to using 'int' for everything since it's the CPU's preferred size, but basically, just use the type of the size you need, and let the compiler worry about optimisations :D
It depends. If you are running on a small processor with small caches, then choosing the smallest data size may make sense. If you have large amounts of data, for example millions of samples which each need 8 bit precision, then using the smallest data size makes sense. In most other cases, leave it to the compiler.
In a 32-bit CPU, packing four 8-bit bytes into a 32-bit word can improve memory access time, since four bytes can be fetched at once. However, now to manipulate a single byte the CPU has to perform additional shifts & masks, etc. So either way packing 4 bytes into a word, or leaving each byte unpacked (using 32-bits for each 8-bit byte) has pros and cons.
Assuming we're talking about C or C++, an optimizing compiler will usually make the right decisions for you, but you can control this behavior explicitly if you must by doing your own packing into structs, etc.
However, there are other BETTER reasons to use a type that matches the domain of your data: clarity, maintainability, etc. I think these really trump optimization concerns 99% of the time.
精彩评论