.NET 优化的 Int32 [英] .NET Optimized Int32

查看:26
本文介绍了.NET 优化的 Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 70-536 培训套件时,它指出:

While reading through the 70-536 training kit, it states:

运行时优化性能32 位整数类型 (Int32),所以将这些类型用于计数器和其他经常访问的积分变量.

The runtime optimizes the performance of 32-bit integer types (Int32), so use those types for counters and other frequently accessed integral variables.

这是否仅适用于 32 位环境?Int64 是在 64 位环境中接管,还是 Int32 仍然是更好的选择?

Does this only apply in a 32 bit environment? Does Int64 take over in a 64 bit environment, or is Int32 still the better choice?

推荐答案

这种说法很有趣.运行时与它没有太大关系.CPU 专为处理 32 位整数而设计,这就是它们使用效率最高的原因.

That's a funny way to put it. The runtime doesn't have much to do with it. The CPU is designed for processing 32-bit integers, which is why they're the most efficient to use.

在 64 位环境中,它再次取决于 CPU.但是,至少在 x86 CPU 上(据我所知,这是 .NET 运行的唯一地方),32 位整数仍然是默认值.寄存器已简单扩展,因此它们可以适合 64 位值.但 32 仍然是默认值.

In a 64-bit environment, it again depends on the CPU. However, on x86 CPU's at least (which, to the best of my knowledge, is the only place .NET runs), 32-bit integers are still the default. The registers have simply been expanded so they can fit a 64-bit value. But 32 is still the default.

所以更喜欢 32 位整数,即使在 64 位模式下也是如此.

So prefer 32-bit integers, even in 64-bit mode.

默认"可能不是正确的词.CPU 只支持许多指令,这些指令定义了它可以处理哪些数据类型,哪些不可以.那里没有默认".但是,通常存在 CPU 设计为有效处理的数据大小.在 x86 上,在 32 位和 64 位模式下,即 32 位整数.64 位值通常不会更昂贵,但它们确实意味着更长的指令.我也相信至少支持 64 位的 Pentium 4 在 64 位操作上要慢得多,尽管在最近的 CPU 上,这部分应该不是问题.(但指令大小可能还在)

"default" is probably not the right word. The CPU just supports a number of instructions, which define which data types it can process, and which it can not. There is no "default" there. However, there is generally a data size that the CPU is designed to process efficiently. And on x86, in 32 and 64-bit mode, that is 32-bit integers. 64-bit values are generally not more expensive, but they do mean longer instructions. I also believe that at least the 64-bit capable Pentium 4's were significantly slower at 64-bit ops, although on recent CPU's, that part shouldn't be an issue. (But the instruction size may still be)

小于 32 位的值更令人惊讶.是的,传输的数据较少,这很好,但 CPU 仍然一次抓取 32 字节.这意味着它必须屏蔽掉部分值,因此它们会变得更慢.

Smaller than 32-bit values are somewhat more surprising. Yes, there is less data to transfer, which is good, but the CPU still grabs 32-byte at a time. Which means it has to mask out part of the value, so these become even slower.

这篇关于.NET 优化的 Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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