(如何)Java JIT编译器优化我的代码? [英] (How) does the Java JIT compiler optimize my code?

查看:196
本文介绍了(如何)Java JIT编译器优化我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写相当低级别的代码,必须针对速度进行高度优化。每个CPU周期都很重要。因为代码是用Java编写的,所以我不能像C中那样写低级别,但是我想从虚拟机中获取所有内容。

I'm writing fairly low level code that must be highly optimized for speed. Every CPU cycle counts. Since the code is in Java I can't write as low level as in C for example, but I want to get everything out of the VM that I can.

I处理一个字节数组。我的代码有两个部分,我现在主要感兴趣。第一个是:

I'm processing an array of bytes. There are two parts of my code that I'm primarily interested in at the moment. The first one is:

int key =  (data[i]     & 0xff)
        | ((data[i + 1] & 0xff) <<  8)
        | ((data[i + 2] & 0xff) << 16)
        | ((data[i + 3] & 0xff) << 24);

,第二个是:

key = (key << 15) | (key >>> 17);

从性能来看,我猜这些陈述没有按照我的预期进行优化。第二个语句基本上是 ROTL 15,键。第一个语句将4个字节加载到int中。 0xff 掩码仅用于补偿如果访问的字节恰好是负数,则隐式转换为int所产生的添加符号位。这应该很容易转换为高效的机器代码,但令我惊讶的是,如果我删除掩码,性能会上升。 (这当然会破坏我的代码,但我很想知道会发生什么。)

Judging from the performance I'm guessing that these statements aren't optimized the way I expect. The second statement is basically a ROTL 15, key. The first statement loads 4 bytes into an int. The 0xff masks are there only to compensate for the added sign bits resulting from the implicit cast to int if the accessed byte happens to be negative. This should be easy to translate to efficient machine code, but to my surprise performance goes up if I remove the masks. (Which of course breaks my code, but I was interested to see what happens.)

这里发生了什么?最常见的Java VM是否会在JIT期间以期望优秀的C ++编译器优化等效C ++代码的方式优化此代码?我可以影响这个过程吗?设置 -XX:+ AggressiveOpts 似乎没什么区别。

What's going on here? Do the most common Java VMs optimize this code during JIT in the way one would expect a good C++ compiler to optimize the equivalent C++ code? Can I influence this process? Setting -XX:+AggressiveOpts seems to make no difference.

(CPU:x64,平台:Linux / HotSpot )

(CPU: x64, Platform: Linux/HotSpot)

推荐答案

你如何测试表现?

这是一篇好文章:

http ://www.ibm.com/developerworks/java/library/j-benchmark1/index.html

http://www.ibm.com/developerworks/java/library/j-benchmark2/index.html

http://ellipticgroup.com/html/benchmarkingArticle.html

这篇关于(如何)Java JIT编译器优化我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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