在C#中是字节vs短vs int(沿无符号变化)? [英] Byte vs Short vs Int (Along With The Unsigned Variation) In C#?

查看:92
本文介绍了在C#中是字节vs短vs int(沿无符号变化)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,只要内存大小不是一个大问题,使用int而不是字节或short总是更好,因为CPU实际上更容易处理int(CPU需要做一些额外的工作以处理字节和短裤).在C#中是真的吗?

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

推荐答案

它更多地取决于处理器而不是语言. 8位微控制器几乎可以肯定地比访问8位字符的速度快一个32位int.

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.

意识到这一限制后,算法设计人员就可以进行相应的计划: Rijndael 的原因之一赢得AES竞赛的原因是,除了关心32位或更大处理器的执行速度之外,设计人员还计划尽可能快地制作8位版本.

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.

但是对于32位和64位微处理器,数据对齐和批量数据访问是关键:int访问通常比charlong long (64位),对于某些系统,可能仍然更快. (但是,在32位计算机上执行64位操作要慢得多,因此,当数据实际上在64位中更有意义时,使用64位数据类型最有意义.)

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

这篇关于在C#中是字节vs短vs int(沿无符号变化)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆