Efficient code: short and integer data types in VB.Net

I am writing an application where performance is quite critical. I'm a little confused about what is the most efficient data type for x64 processors.

MDSN says that in some cases, a common language runtime can quickly combine short variables and save memory consumption. but also that the Integer data type provides optimal performance for a 32-bit processor

I use a huge amount of data (an average of about 5 million values ​​in a jagged array [10 or more] [30] [128,128]) to generate real-time raster images (heat map of data values). All data points are integers from 200 to 3500, so I can use short or integer numbers. What would be most effective?

Thanks.

+6
source share
2 answers

The Int32 type is most effective for common variables, such as loop counters, in both 32-bit and 64-bit applications.

When you process arrays of large data arrays, the read / write efficiency of one value does not matter much, it is important to have access to the data in order to get as few misses in the memory cache as possible. Skipping a memory cache is very expensive compared to accessing cached memory. (In addition, a page error (memory replaced by disk) is very expensive compared to skipping the memory cache.)

To avoid gaps in the cache, you can store data as compact as possible, and when processing data, you can access it as linearly as possible so that the available memory area is as small as possible.

Using Int16 most likely be more efficient than Int32 for any array large enough to span multiple cache blocks, and a cache block is usually only a few kilobytes.

Since your values ​​can be stored in just 12 bits, it can be even more efficient to store each value in 1.5 bytes, although this means more processing data. Reducing 25% of the data size can lead to additional processing.

+1
source

Generally, the less memory a variable uses, the faster it will be processed and you will have better memory management because your application will use less.

A short need for half the needs of an integer memory size, if you only need a 16-bit number, and you are sure that it will never be more, use Short.

0
source

All Articles