I was told, that as long as memory size was not a huge concern, it is always better to use an int instead of a byte or short because it is actually easier for the CPU to handle an int (the CPU needs to do extra stuff to work with bytes and shorts). Is this true in C#?
-
The CPU doesn't care what language your code was in... – user541686 Feb 21 '11 at 10:20
-
Ok, then is it true in any language? – ryanzec Feb 21 '11 at 10:21
-
Yeah, because it has nothing to do with the language... 32-bit CPUs work well with 32-bit integers, 64-bit CPUs with 64-bit integers. – user541686 Feb 21 '11 at 10:25
-
possible duplicate http://stackoverflow.com/questions/2594013/int-short-byte-performance-in-back-to-back-for-loops – Kris Ivanov Feb 21 '11 at 10:31
-
In .net booleans get converted into 'native proc data size' so 32bits for 32bits machines and 64 for.. My guess would be that everything smaller then an integer would be stored as an integer. – CodingBarfield Feb 21 '11 at 10:43
1 Answers
It depends more on the processor than on the language. An 8-bit microcontroller will almost certainly be able to access an 8-bit char faster than a 32-bit int.
Being aware of this limitation allows algorithm designers to plan accordingly: One of the reasons why Rijndael won the AES competition is because the designers had planned for making 8-bit versions as fast as possible, in addition to caring about execution speed on 32-bit or larger processors.
But for 32-bit and 64-bit microprocessors, data alignment and bulk data access is key: int
accesses are frequently much faster than char
accesses, and long long
(64 bit) may be faster still for some systems. (But the 64-bit operations on a 32-bit machine are much slower, so using 64-bit datatypes makes most sense when the data actually makes more sense in 64 bits.)

- 102,305
- 22
- 181
- 238