Java JRE 与 GCJ

发布于 2024-09-05 02:50:17 字数 1174 浏览 14 评论 0原文

我用 Java 编写的速度测试得到了以下结果:

Java

real        0m20.626s
user        0m20.257s
sys         0m0.244s

GCJ

real        3m10.567s
user        3m5.168s
sys         0m0.676s

那么 GCJ 的目的是什么?有了这个结果,我确信我不会用 GCJ 编译它!

我在 Linux 上测试了这个,Windows 上的结果可能比这更好吗?

这是应用程序中的代码:

public static void main(String[] args) {
    String str = "";
    System.out.println("Start!!!");
    for (long i = 0; i < 5000000L; i++) {
        Math.sqrt((double) i);
        Math.pow((double) i, 2.56);
        long j = i * 745L;
        String string = new String(String.valueOf(i));
        string = string.concat(" kaka pipi"); // "Kaka pipi" is a kind of childly call in Dutch. 
        string = new String(string.toUpperCase());
        if (i % 300 == 0) {
            str = "";
        } else {
            str += Long.toHexString(i);
        }
    }
    System.out.println("Stop!!!");
}

我用 GCJ 编译如下:

gcj -c -g -O Main.java
gcj --main=speedtest.Main -o Exec Main.o

并运行如下:

time ./Exec                     // For GCJ
time java -jar SpeedTest.jar    // For Java

I have this results from a speed test I wrote in Java:

Java

real        0m20.626s
user        0m20.257s
sys         0m0.244s

GCJ

real        3m10.567s
user        3m5.168s
sys         0m0.676s

So, what is the purpose of GCJ then? With this results I'm sure I'm not going to compile it with GCJ!

I tested this on Linux, are the results in Windows maybe better than that?

This was the code from the application:

public static void main(String[] args) {
    String str = "";
    System.out.println("Start!!!");
    for (long i = 0; i < 5000000L; i++) {
        Math.sqrt((double) i);
        Math.pow((double) i, 2.56);
        long j = i * 745L;
        String string = new String(String.valueOf(i));
        string = string.concat(" kaka pipi"); // "Kaka pipi" is a kind of childly call in Dutch. 
        string = new String(string.toUpperCase());
        if (i % 300 == 0) {
            str = "";
        } else {
            str += Long.toHexString(i);
        }
    }
    System.out.println("Stop!!!");
}

I compiled with GCJ like this:

gcj -c -g -O Main.java
gcj --main=speedtest.Main -o Exec Main.o

And ran like this:

time ./Exec                     // For GCJ
time java -jar SpeedTest.jar    // For Java

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

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

发布评论

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

评论(6

靑春怀旧 2024-09-12 02:50:17

GCJ 已过时。它很久以前就开始了,因为人们想要一个 Sun JDK 的开源替代品,但它从来都不是特别好。现在 Sun 开源了他们的 JDK,绝对没有理由使用 GCJ(但它仍然潜伏在一些 Linux 发行版中)。

GCJ is obsolete. It was started a long time ago because people wanted an open-source alternative to the Sun JDK, and it was never particularly good. Now that Sun open-sourced their JDK, there's absolutely no reason to use GCJ (but it still lurks in some Linux distros).

折戟 2024-09-12 02:50:17

当您进行 AOT(提前)编译且几乎没有优化 (-O) 时,这不是一个公平的比较。至少尝试-O2。

它也不是那么简单,因为在一次人为的测试中,一个比另一个更快。 AOT 编译在某些场景下效果最好。 JVM 在其他方面工作得更好,这也在很大程度上取决于 JVM 的质量。在现实世界中,ecj 在 AOT 编译时构建 OpenJDK 的速度明显快于在 JVM 上运行时的速度。对于长时间运行的应用程序,JVM 可能会获胜,因为它可以利用提前不可能实现的动态优化。 ecj 受到影响是因为在它编译的短时间内,JVM 仍在解释代码。当 HotSpot 确定代码值得时(“热点”),它会编译和优化代码。

顺便说一句,这是过时的常见问题解答。 GCJ 支持大部分 1.5,当然足以构建 OpenJDK。如果 GCJ 仍然“潜伏在某些 Linux 发行版中”,那么一开始就不可能构建 OpenJDK。

It's not a fair comparison when you do the AOT (Ahead-Of-Time) compile with little optimisation (-O). Try at least -O2.

It's also not as simple as one is faster than the other on a single contrived test. AOT compilation works best in some scenarios. JVMs work better in others, and it also depends heavily on the quality of the JVM. In the real world, ecj is noticeably faster at building OpenJDK when AOT-compiled rather than when running on the JVM. For long-running applications, the JVM is likely to win because it can make use of dynamic optimisations not possible ahead-of-time. ecj suffers because for the short period it's compiling, the JVM is still interpreting code. HotSpot compiles and optimises code when it determines it's worthwhile (a 'hot spot').

BTW, it's the FAQ that's out of date. GCJ supports most of 1.5, certainly enough to build OpenJDK. Without GCJ still 'lurking in some Linux distros', it wouldn't be possible to build OpenJDK in the first place.

誰認得朕 2024-09-12 02:50:17

在 x86 和 AMD64 上,Hotspot 仅使用 SSE 进行浮点运算,但我发现在 x86 上 gcj 似乎不支持 SSE 并使用慢得多的 387 指令:

gcj -O3 -mfpmath=sse --main=Bench Bench.java -o Bench
jc1: warning: SSE instruction set disabled, using 387 arithmetics
/tmp/ccRyR50H.i:1: warning: SSE instruction set disabled, using 387 arithmetics

因此这可以解释速度差异。

请注意,当 GCC 确实使用 SSE 时,它在浮点上的性能大大优于 Hotspot,因为 GCC 生成 SIMD 指令,而 Hotspot 仅使用未打包的 SSE 算术。

On x86 and AMD64, Hotspot only uses SSE for floating point, but I see that on x86 gcj doesn't seem to support SSE and uses the much slower 387 instructions:

gcj -O3 -mfpmath=sse --main=Bench Bench.java -o Bench
jc1: warning: SSE instruction set disabled, using 387 arithmetics
/tmp/ccRyR50H.i:1: warning: SSE instruction set disabled, using 387 arithmetics

so that could explain the speed difference.

Note that when GCC does use SSE, it greatly outperforms Hotspot on floating point, since GCC generates SIMD instructions while Hotspot only uses the unpacked SSE arithmetic.

天气好吗我好吗 2024-09-12 02:50:17

OpenJDK 的本机 Java 编译器本身是用 Java 编写的;因此,您需要一个可以工作的旧版 Java 才能构建新版本。

如果您从头开始在一个没有现成的 JDK 二进制文件的平台上工作(或者如果您正在某些自由软件项目中工作,其章程禁止使用专有的构建依赖项),那么 GCJ(或其某些底层)组件)可能是解决先有鸡还是先有蛋问题的一种潜在解决方案,即获得一个可行的(尽管有些低效的)引导 Java,以便继续构建更理想的 OpenJDK。

事实上,早在 OpenJDK 首次发布时,就花费了大量精力(通过 IcedTea 项目)来修复 GCJ,以使其能够胜任任务。

OpenJDK's native Java compiler is, itself, written in Java; as a consequence, you need a working previous version of Java in order to build a new version.

If you're starting from scratch on a platform for which no existing JDK binaries are readily available (or if you're working within certain Free Software projects whose charters prohibit the use of proprietary build dependencies), then GCJ (or some of its underlying components) can be one potential solution to the chicken-and-egg problem of getting a working, albeit somewhat inefficient bootstrap Java in place, in order to move on to build the more desirable OpenJDK.

In fact, back when OpenJDK was first announced, significant effort (via the IcedTea project) was spent in fixing up GCJ to get it to the point where it was up to the task.

站稳脚跟 2024-09-12 02:50:17

“那么GCJ的目的是什么?”

有些人指出你的“基准”不公平。然而,即使是这样,我仍然可以看到 GCJ 的用途。假设您想用 Java 编写内核。使用 JVM,您必须将 VM 移植到独立环境,然后您必须用 C 编写低级代码。使用 AOT 编译器,您可以使用一些粘合代码解决此问题,然后可以执行低级代码Java 中的级别代码也是如此。在这种情况下不需要移植编译器。

此外,我们必须将技术与基准分开。如果我们投入足够的开发精力,也许 AOT 技术会比 JIT 技术更强大。

"So, what is the purpose of GCJ then?"

Some have pointed that your "benchmark" isn't fair. However, even if it was, I can still see a use for GCJ. Suppose you want to write a kernel in Java. With a JVM, you have to port the VM to a standalone environment, and then you would have to write the low lever code in C. With an AOT compiler, you can work around this issue with some glue code and then can do the low level code in Java too. No need to port the compiler in this case.

Also, we have to separate the technique from the benchmarks. Perhaps the AOT technique can be more powerful than the JIT technique provided that we invest enough development effort in it.

夜夜流光相皎洁 2024-09-12 02:50:17

您偶然发现了“不惜任何代价的软件自由!”的另一个产品。的思路。创建 GCJ 是为了允许编译和执行 Java 代码,而不依赖于任何被 GNU 项目视为“非自由”的东西。

如果您重视软件自由度,足以承受 12 倍的性能损失,那么无论如何,就去吧!

我们其他人会很乐意使用 Sun(呃,Oracle)令人难以置信的 HotSpot JVM。

另请参阅:GCJ 常见问题解答:“我刚刚编译并测试了我的 Java 应用程序,它的运行速度似乎比 XXX JIT JVM 慢。我能做些什么来让它运行得更快吗?”

另外:“它已与 GNU 合并类路径并支持大多数 1.4 库以及一些 1.5 新增内容。”所以它也有点过时了。

You have stumbled onto another product of the "Software Freedom at any cost!" line of thinking. GCJ was created to allow compilation and execution of Java code without depending on anything deemed "non-free" by GNU project.

If you value software freedom enough to take a 12x performance hit, then by all means, go for it!

The rest of us will happily use Sun's (er, Oracle's) incredible HotSpot JVM.

See also: The GCJ FAQ: "I have just compiled and benchmarked my Java application and it seems to be running slower than than XXX JIT JVM. Is there anything I can do to make it go faster?"

Also: "It has been merged with GNU Classpath and supports most of the 1.4 libraries plus some 1.5 additions." So it's just a bit out of date as well.

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