Question

So, if I (as the programmer) know that my value will never exceed a small number (say 100) because it is really just a counter-controlled loop or whatever, may I use short or char instead of int for the minute memory savings?

Was it helpful?

Solution

You can, and this will likely work. As FrustratedWithFormsDesigner mentioned you will need to be sure of bounds checking.

Another important factor is that as delnan said registers are fixed size. But using them as a smaller type may mean using different, less-efficient instructions if the variable's size is not the computer's word size.

Most of the time there is not much to be gained from using variable sizes smaller than the machine's word size. Embedded systems may benefit, and if a file format specifies specific field sizes then of course use those. Another thing to keep in mind is memory alignment, a 16 bit variable may be aligned on a 32 or 64 bit memory boundary with unused space between variables. Or it may not. Or it may be a compiler flag.

Edit: the comments brought up a good point. Even with a 64 bit word size, int is still likely 32 bits (C/C++, desktop platforms). The reasons are historical and out of context here. However, a modern 64 bit CPU such as the Intel Core i7 is heavily optimized for 32 and 64 bit arithmetic. Any CPU should be fast when using ints the same size as its native word size. However, there is a lot of 32 bit software out there that these CPUs need to be able to run and run well. Working with ints smaller than 32 bits may or may take a performance hit, as you are now outside the realm of what the CPU is optimized for. In practice I would expect any performance hit to be minimal, and the memory difference would be negligible as well. File this one under "premature optimization."

OTHER TIPS

I suppose you could. Of course, if you do that you should also include code that validates your variable so that if an input is greater than 100, you get a sensible error message. And it's also a good idea to add comments to explain why you're using char instead of int (if that's what you end up doing). It might not be obvious to everyone. Are you working in an environment where memory is so tight that this will make a significant difference to your program? Are you doing this for educational reasons?

Yes This is especially useful in for loops. People often just use int, or allow var to determine what the variable type should be. This is inefficient (but not terribly so), but for people like me who like to get every ounce of performance and are just tickled pink by using the most perfect datatype for a situation, this is awesome.

for(SByte i = 0; i <= 10; i++)
{
...code
}

SByte allows -128 to 127. I love this little datatype because it saves on memory and is the most appropriate datatype for the situation where warranted.

However, integer, in some languages, is the fastest datatype for arithmetic operations, even when it is way to large of a container for what you need.

Arithmetic operations are faster with integral types than with other data types. They are fastest with the Integer and UInteger types in Visual Basic. 1 (msdn.com)

So in loops, yes, use this. When doing arithmetic, however, air on the side of caution and use an int unless you research the language you are using first to see what the fastest type is.

--Adding another example I gave in the comments--

You have a class that has a 100 ints in it, but none of the ints will ever go above a value of 10. Every time you create an object of that class you are blocking off 32 bits per int creating 32*100=3200 bits. If you replaced those ints with sBytes you are now blocking off 8*100=800. You are saving 2400 bits for use anywhere else.

Licensed under: CC-BY-SA with attribution
scroll top