我应该在 64 位语言中使用 'long' 而不是 'int' 固定类型大小(如 Java、C#) [英] Should I use 'long' instead of 'int' on 64-bits in langs with fixed type size (like Java, C#)

查看:31
本文介绍了我应该在 64 位语言中使用 'long' 而不是 'int' 固定类型大小(如 Java、C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 10 年,甚至 5 年内,将没有 [Edit2: 服务器或台式机] 32 位 CPU.

In 10, or even 5 years there will be no [ server or desktop] 32-bit CPUs.

那么,使用 int(32 位)比 long(64 位)有什么优势?
使用 int 有什么缺点吗?

So, are there any advantages in using int (32bit) over long (64bit) ?
And are there any disadvantages in using int ?

  1. 通过 10 或 5 年我的意思是在绝大多数使用这些语言的地方

  1. By 10 or 5 years I meant on vast majority of places where those langs are used

我的意思是默认使用哪种类型.这几天我什至不会考虑是否应该使用 short 作为循环计数器,只需 for(int i....同样的方式 long 计数器已经赢了

I meant which type to use by default. This days I won't even bother to think if I should use short as cycle counter, just for(int i.... The same way long counters already win

寄存器已经是 64 位的,32 位类型已经没有增益了.而且我认为 8 位类型有一些损失(您必须对更多位进行操作,然后才能使用)

registers are already 64-bit, there is already no gain in 32 bit types. And I think some loss in 8 bit types (you have to operate on more bits then you're using)

推荐答案

如果您使用的是 64 位处理器,并且您已经为 64 位编译了代码,那么至少在某些时候,long 可能更有效,因为它匹配寄存器大小.但是,这是否真的会对您的计划产生很大影响是值得商榷的.此外,如果您到处使用 long,您通常会使用更多的内存 - 在堆栈和堆上 - 这可能会对性能产生负面影响.有太多变量无法确定您的程序在默认情况下使用 long 而不是 int 会执行得如何.它可能更快,也可能更慢,这是有原因的.这可能是一次彻底的清洗.

If you're on a 64-bit processor, and you've compiled your code for 64-bit, then at least some of the time, long is likely to be more efficient because it matches the register size. But whether that will really impact your program much is debatable. Also, if you're using long all over the place, you're generally going to use more memory - both on the stack and on the heap - which could negatively impact performance. There are too many variables to know for sure how well your program will perform using long by default instead of int. There are reasons why it could be faster and reasons why it could be slower. It could be a total wash.

如果您不关心整数的大小,通常要做的就是使用 int.如果需要 64 位整数,则使用 long.如果您想使用更少的内存并且 int 远远超出您的需要,那么您可以使用 byteshort.

The typical thing to do is to just use int if you don't care about the size of the integer. If you need a 64-bit integer, then you use long. If you're trying to use less memory and int is far more than you need, then you use byte or short.

x86_64 CPU 将被设计为高效处理 32 位程序,因此使用 int 不会严重降低性能.当您在 64 位 CPU 上使用 64 位整数时,由于更好的对齐,某些事情更快,但由于内存需求增加,其他事情会变慢.并且可能还涉及多种其他因素,这些因素肯定会影响任一方向的性能.

x86_64 CPUs are going to be designed to be efficient at processing 32-bit programs and so it's not like using int is going to seriously degrade performance. Some things will be faster due to better alignment when you use 64-bit integers on a 64-bit CPU, but other things will be slower due to the increased memory requirements. And there are probably a variety of other factors involved which could definitely affect performance in either direction.

如果您真的想知道在您的特定环境中哪个对您的特定应用程序会做得更好,您将需要对其进行分析.这并不是一个明显优于另一个的情况.

If you really want to know which is going to do better for your particular application in your particular environment, you're going to need to profile it. This is not a case where there is a clear advantage of one over the other.

就个人而言,我建议您在不关心整数的大小时遵循使用 int 的典型路线,并在关心时使用其他类型.

Personally, I would advise that you follow the typical route of using int when you don't care about the size of the integer and to use the other types when you do.

这篇关于我应该在 64 位语言中使用 'long' 而不是 'int' 固定类型大小(如 Java、C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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