I'm writing an application where performance is fairly critical. I'm a bit confused as to which is the most efficient data type for x64 CPUs.
MDSN says that "In some cases, the common language runtime can pack your Short variables closely together and save memory consumption." but also that "The Integer data type provides optimal performance on a 32-bit processor"
I'm using a huge amount of data (average around 5 million values in a jagged array[10 or more][30][128,128]) to generate bitmaps in real time (heat maps of the data values). All of the data points are whole numbers between 200 and 3500 so I can use short or integer. Which would be most efficient?
Thanks.
The Integer data type provides optimal performance on a 32-bit processor. The other integral types are slower to load and store from and to memory. The default value of Integer is 0.
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte , the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
The two fundamental data types in Visual Basic are value types and reference types. Primitive types (except strings), enumerations, and structures are value types. Classes, strings, standard modules, interfaces, arrays, and delegates are reference types.
Remarks. Use the Short data type to contain integer values that do not require the full data width of Integer . In some cases, the common language runtime can pack your Short variables closely together and save memory consumption. The default value of Short is 0.
The Int32
type is most efficient for regular variables, for example loop counters, both in 32 bit and 64 bit applications.
When you handle large arrays of data the efficiency of reading/writing a single value doesn't matter much, what matters is to access the data so that you get as few memory cache misses as possible. A memory cache miss is very expensive compared to an access to cached memory. (Also, a page fault (memory swapped to disk) is very expensive compared to a memory cache miss.)
To avoid cache misses you can store the data as compact as possible, and when you process the data you can access it as linearly as possible so that the memory area that you access is as small as possible.
Using Int16
is most likely to be more efficient than Int32
for any array large enough to span multiple cache blocks, and a cache block is generally just a few kilobytes.
As your values are possible to store in just 12 bits, it might even be more efficient to store each value in 1.5 bytes eventhough that means more processing to handle the data. The reduction of 25% of the data size might more than make up for the extra processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With