将在 64 位 java 中使用 longs 而不是 ints 受益 [英] Will using longs instead of ints benefit in 64bit java

查看:22
本文介绍了将在 64 位 java 中使用 longs 而不是 ints 受益的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 64 位 VM 中,考虑到 Java 中的 long 是 64 位,因此拉取和处理 64 位,使用 longs 而不是 ints 会在性能方面做得更好位字可能比在 64 位系统中提取 32 位字更快.(我期待有很多 NO,但我一直在寻找详细的解释).

In a 64 bit VM, will using longs instead of ints do any better in terms of performance given that longs are 64 bits in java and hence pulling and processing 64 bit word may be faster that pulling 32bit word in a 64 bit system. (I am expecting a lot of NOs but I was looking for a detailed explanation).

编辑:我的意思是在 64 位系统中拉取和处理 64 位字可能比拉取 32 位字更快",因为我假设在 64 位系统中,拉取 32位数据要求您首先获得 64 位字,然后屏蔽前 32 位.

EDIT: I am implying that "pulling and processing 64 bit word may be faster that pulling 32bit word in a 64 bit system" because I am assuming that in a 64 bit system, pulling a 32 bit data would require you to first get the 64 bit word and then mask the top 32 bits.

推荐答案

int 使用 long 通常可能会减慢你的速度.

Using long for int probably will slow you down in general.

你最关心的是int在64位CPU上是否需要额外的处理时间.这在现代流水线 CPU 上是极不可能的.我们可以用一个小程序轻松测试.它操作的数据应该足够小以适合 L1 缓存,以便我们测试这个特定问题.在我的机器上(64 位 Intel Core2 Quad)基本上没有区别.

You immediate concern is whether int on 64 bit CPU requires extra processing time. This is highly unlikely on a modern pipelined CPU. We can test this easily with a little program. The data it operates on should be small enough to fit in L1 cache, so that we are testing this specific concern. On my machine (64bit Intel Core2 Quad) there's basically no difference.

在实际应用中,大多数数据不能驻留在 CPU 缓存中.我们必须担心将数据从主内存加载到缓存,这相对非常慢并且通常是一个瓶颈.这种加载以缓存行"为单位进行,即 64 字节或更多,因此加载单个 longint 将花费相同的时间.

In a real app, most data can't reside in CPU caches. We must worry about loading data from main memory to cache which is relatively very slow and usually a bottleneck. Such loading works on the unit of "cache lines", which is 64 bytes or more, therefore loading a single long or int will take the same time.

然而,使用long会浪费宝贵的缓存空间,因此缓存未命中会增加,这是非常昂贵的.Java 的堆空间也有压力,所以 GC 活动会增加.

However, using long will waste precious cache space, so cache misses will increase which are very expensive. Java's heap space is also stressed, so GC activity will increase.

我们可以通过读取具有相同元素数量的巨大long[]int[] 数组来证明这一点.它们比缓存可以包含的要大得多.长版本在我的机器上多花 65% 的时间.测试受限于memory->cache的吞吐量,long[]内存体积大100%.(为什么不多花 100% 的时间超出我的范围;显然其他因素也在起作用)

We can demonstrate this by reading huge long[] and int[] arrays with the same number of elements. They are way bigger than caches can contain. The long version takes 65% more time on my machine. The test is bound by the throughput of memory->cache, and the long[] memory volume is 100% bigger. (why doesn't it take 100% more time is beyond me; obviously other factors are in play too)

这篇关于将在 64 位 java 中使用 longs 而不是 ints 受益的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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