I am going to be using very small numbers in really large quantities (As in each int will be between 1 and 10 and have 100s of variables like this at a time) for the first time, and I was wondering if there was any significant performance differences in using 16 bit integers VS 32 bit. Most of my users will be using 64 bit processors but some will have 32 bit.
Share
CPU
When working with individual numbers, there will possibly be a slight improvement using a more compact representation. That is,
might be slightly faster than
depending on your algorithms. The reason is that more data can fit in CPU cache when the individual numbers are smaller, and ultimately less data has to be transferred from RAM to the CPU.
On the other hand, it might be slightly slower due to reading/writing data that is not aligned to a 64-bit address boundary.
Both factors work against each other. Measure your use cases to find out if you’re better or worse off from a CPU perspective.
In RAM
Using a
List<ushort>instead of aList<ulong>will save 75% in terms of memory cost. If this prevents swapping, it will benefit your application.On Disk
If you are saving to a database, and if your working set is too large to fit in RAM available to the database engine, using the smaller size can have a huge difference on performance because more records fit onto fewer sectors of your disk reducing disk seek time (assuming no SSD) and increasing the amount of data points that can be transferred through the IO channel per unit time.
Caveat
Note that all of this only matters if really large quantities is true in comparison to the processing power of your computer. If you have enough RAM to keep all data in memory, you will not swap. If your database server and/or storage controller cache can keep your working set in memory, disk storage size considerations will matter little.
Bottom Line
Using a smaller data type will only hurt you if you were wrong about the range of values you will process, and you suddenly need a larger data type to hold all values.
Do a quick prototype of your key algorithms. Perform some measurements using various choices of ushort, uint, ulong.
If your “100’s of variables” each only contain a single number (as opposed to, say, a list with lots and lots of numbers), none of this matters. Only when you start to put pressure on your computer (maybe in 10’s to 100’s of millions of data points or more, depending on what you are doing) will these optimizations matter in a real way to your users.