开发者

Byte vs Short vs Int (Along With The Unsigned Variation) In C#?

开发者 https://www.devze.com 2023-02-12 22:12 出处:网络
I was told, that as long as memory size was not a huge concern, it is alway开发者_C百科s better to use an int instead of a byte or short because it is actually easier for the CPU to handle an int (the

I was told, that as long as memory size was not a huge concern, it is alway开发者_C百科s 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#?


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消