64 位 JVM 中的 int 数据类型。是不是更“低效”?比长?

发布于 2024-09-01 09:59:21 字数 270 浏览 3 评论 0原文

我听说在 32 位系统上使用 short 比使用 int 效率更低。这对于 64 位系统上的 int 是一样的吗?

Python 最近(?)基本上将 intlong 合并,并且基本上具有单一数据类型 long,对吧?如果您确定您的应用程序。那么只能在 64 位上运行,是否可以想象(可能是一个好主意)在 Java 中对所有内容都使用 long ?

I heard that using shorts on 32bit system is just more inefficient than using ints. Is this the same for ints on a 64bit system?

Python recently(?) basically merged ints with long and has basically a single datatype long, right? If you are sure that your app. will only run on 64bit then, is it even conceivable (potentially a good idea) to use long for everything in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-09-08 09:59:21

Python long 具有任意精度,它不是 64 位。 Python 3将long更改为int,因此现在只有一种任意精度的整数类型,这为程序员节省了大量的工作。 Java的int是32位的,long是64位的。 64 位处理器在处理 64 位整数时通常比 32 位处理器表现更好。

在 64 位平台上使用 32 位整数并不昂贵,至少在 x64 中是这样。在 Java 中仅仅出于性能原因选择 32 位而不是 64 位 int 是没有意义的,反之亦然。如果您使用两个 32 位 int 而不是 1 个 64 位 int,无论如何都会变慢。同样,如果您在本来可以使用 32 位的地方使用 64 位,则在某些平台上速度会更慢。换句话说,为您的问题域使用正确的数据类型。

A Python long has arbitrary precision, it's not 64-bit. Python 3 changed long to int, so now there is only one integral type of arbitrary precision, this saves a lot of work for the programmer. Java's int is 32-bit, it's long is 64-bit. A 64-bit processor can generally perform better than a 32-bit processor on a 64-bit integer.

Using 32-bit integers on a 64-bit platform isn't expensive, at least not in x64. It makes no sense to choose a 32-bit over 64-bit int or vise-versa in Java just for performance reasons. If you used two 32-bit ints instead of one 64-bit int, it would be slower no matter what. Similarily, if you use 64-bit where you could have used 32-bit, it will be slower on some platforms. In other words, use the right data type for your problem domain.

弥枳 2024-09-08 09:59:21

如果您确定您的应用程序。那么只能在 64 位上运行,是否可以想象(可能是一个好主意)对 Java 中的所有内容使用 long ?

坏主意,IMO

即使在 64 位 JVM 上使用 intlong 之间存在差异(我认为这是高度怀疑) ,它对于整个应用程序(包括您所依赖的库)的重要性不足以保证对所有内容使用 long

并且存在一些潜在的问题:

  • 您将使用更多的内存,至少对于 long[]int[] 相比。
  • 如果您关于永远不需要在 32 位平台上运行的假设被证明是错误的,您将会后悔。

(我认为不会有显着性能差异的原因是,如果存在问题,则问题将是获取和存储未对齐的 int-32。但如果是这样,JVM 设计者可能会确保int-32 字段始终在 64 位字地址上对齐,JVM 通常已经在 32 位架构上使用 int-8 和 int-16 执行此操作...)

If you are sure that your app. will only run on 64bit then, is it even conceivable (potentially a good idea) to use long for everything in Java?

Bad idea, IMO

Even if there is a difference between using int and long on a 64-bit JVM (which I think is highly doubtful), it won't be significant enough over the entire application, including the libraries that you depend on to warrant using long for everything.

And there are some potential problems:

  • You will use more memory, at least for long[] versus int[].
  • You will regret it if your assumption about never needing to run on a 32-bit platform turns out to be wrong.

(The reason I think there won't be a significant performance difference is that the issue, if there is one, will be fetching and storing non-aligned int-32s. But if that is so, the JVM designers are likely to ensure that int-32 fields are always aligned on 64-bit word addresses. JVMs typically do this already with int-8 and int-16 on 32bit architectures ... )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文