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

笑着哭i 提交于 2019-12-05 23:56:22

问题


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#?


回答1:


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



来源:https://stackoverflow.com/questions/5064712/byte-vs-short-vs-int-along-with-the-unsigned-variation-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!