Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 753
Absinthe Avatar asked Sep 23 '15 09:09

Absinthe


People also ask

What is integer data type in VB?

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.

What is short integer data type?

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.

What are the two types of data in Visual Basic?

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.

What is short in Visual Basic?

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.


1 Answers

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.

like image 94
Guffa Avatar answered Oct 01 '22 05:10

Guffa