I was only 6 years late, but maybe I can help someone else.
Here are some guidelines I would use:
- If it is likely that the data will not match in the future, use a larger int type.
- If a variable is used as a struct / class field, then by default it will be filled in order to occupy all 32-bit values anyway, so using byte / int16 will not save memory.
- If a variable is short-lived (as inside a function), then smaller data types will help little.
- "byte" or "char" can sometimes better describe the data and can perform compile-time checks to make sure that large values were not assigned to it by accident. For example, if you save the day of the month (1-31) with a byte and try to assign 1000 to it, this will cause an error.
- If a variable is used in an array of about 100 or more, I would use a smaller data type if that makes sense.
- The byte and int16 arrays are not as thread-safe as int (primitive) arrays.
One topic that no one has touched on is the limited processor cache. Smaller programs run faster than larger ones because the processor can accommodate most of the program in faster L1 / L2 / L3 caches.
Using the int type can result in fewer CPU instructions, however, it also means that a higher percentage of data memory will not fit in the CPU cache. Instructions are cheap to execute. Modern processor cores can execute 3-7 instructions per cycle, however, on the one hand, a cache miss can cost 1000-2000 cycles, since it must go to RAM.
When the memory is saved, this also leads to the fact that the rest of the application works better, since it is not squeezed out of the cache.
I ran a quick sum test with random access to random data using both a byte array and an int array.
const int SIZE = 10000000, LOOPS = 80000; byte[] array = Enumerable.Repeat(0, SIZE).Select(i => (byte)r.Next(10)).ToArray(); int[] visitOrder = Enumerable.Repeat(0, LOOPS).Select(i => r.Next(SIZE)).ToArray(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); int sum = 0; foreach (int v in visitOrder) sum += array[v]; sw.Stop();
Here are the results in time (tics): (x86, release mode, without debugger, .NET 4.5, I7-3930k) (the smaller the better)
________________ Array Size __________________ 10 100 1K 10K 100K 1M 10M byte: 549 559 552 552 568 632 3041 int : 549 566 552 562 590 1803 4206
- random access to 1M elements using bytes on my processor increased performance by 285%!
- Anything under 10,000 was barely noticeable.
- int has never been faster than a byte for this basic sum test.
- These values will vary for different processors with different cache sizes.
One final note: sometimes I look at the open source .NET platform to find out what Microsoft experts are doing. The .NET Framework uses byte / int16 surprisingly little. I could not really find.
Sunsetquest Aug 09 '15 at 0:54 2015-08-09 00:54
source share