.NET 整数与 Int16? [英] .NET Integer vs Int16?
问题描述
我有一个可疑的编码实践.
I have a questionable coding practice.
当我需要遍历计数限制低于 32000
的一小部分项目时,我使用 Int16
作为我的 i 变量类型而不是 Integer
.我这样做是因为我认为使用 Int16
比完整的 Integer
更有效.
When I need to iterate through a small list of items whose count limit is under 32000
, I use Int16
for my i variable type instead of Integer
. I do this because I assume using the Int16
is more efficient than a full blown Integer
.
我错了吗?使用 Int16
和 Integer
之间没有有效的性能差异吗?我是否应该停止使用 Int16
并坚持使用 Integer
来满足我所有的计数/迭代需求?
Am I wrong? Is there no effective performance difference between using an Int16
vs an Integer
? Should I stop using Int16
and just stick with Integer
for all my counting/iteration needs?
推荐答案
根据以下参考,runtime 优化了 Int32 的性能,并将它们推荐用于计数器和其他经常访问的操作.
According to the below reference, the runtime optimizes performance of Int32 and recommends them for counters and other frequently accessed operations.
来自书中:MCTS 自定进度培训工具包(考试 70-536):Microsoft® .NET Framework 2.0 — 应用程序开发基础
第 1 章:框架基础"
第 1 课:使用值类型"
Chapter 1: "Framework Fundamentals"
Lesson 1: "Using Value Types"
最佳实践:优化性能内置类型
Best Practices: Optimizing performance with built-in types
运行时优化了 32 位整数类型(Int32 和 UInt32)的性能,因此将这些类型用于计数器和其他经常访问的整数变量.
对于浮点运算,Double 是最有效的类型,因为这些运算由硬件优化.
For floating-point operations, Double is the most efficient type because those operations are optimized by hardware.
此外,同一部分中的表 1-1 列出了每种类型的推荐用途.与本次讨论相关:
Also, Table 1-1 in the same section lists recommended uses for each type. Relevant to this discussion:
- Int16 - 互操作和其他特殊用途
- Int32 - 整数和计数器
- Int64 - 大整数
这篇关于.NET 整数与 Int16?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!