使用16位整数的重要性 [英] The importance of using a 16bit integer

查看:91
本文介绍了使用16位整数的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发人员在编写代码时如何认真考虑使用16位整数?自从进行编程以来,我一直在使用32位整数,我真的不考虑使用16位.

How seriously do developers think about using a 16bit integer when writing code? I've been using 32bit integers ever since I've been programming and I don't really think about using 16bit.

声明32位int十分容易,因为它是大多数语言的默认设置.

Its so easy to declare a 32bit int because its the default for most languages.

除了节省一点内存外,使用16位整数还有什么好处?

Whats the upside of using a 16bit integer apart from a little memory saved?

推荐答案

为大量数值分配内存时,几乎没有多少内存可以节省[读取:50%].常见用途是:

It's hardly a little memory saved [read: 50%] when you allocate memory for a large number of numeric values. Common uses are:

  • COM和外部设备互操作
  • 减少大型阵列的内存消耗,大型阵列每个数字的大小永远不会超过数千个
  • 对象对的唯一散列,其中最多需要约65K个对象.(散列值只能是32位整数,但是请注意,散列表的类型必须转换内部表示的值,因此冲突仍然存在可能,但是相等可以基于精确的哈希匹配)
  • 依靠structs加速算法(较小的值类型在内存中复制时会提高性能)
  • COM and external device interop
  • Reducing memory consumption for large arrays where each number will never exceed a couple thousands in magnitude
  • Unique hashes for pairs of objects, where no more than ~65K objects are needed (hash values can only be 32-bit ints, but note that hash table types must transform the value for internal representations so collisions are still likely, but equality can be based on exact hash matches)
  • Speed up algorithms that rely on structs (smaller sized value types translates to increased performance when they are copied around in memory)

这篇关于使用16位整数的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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