为什么Go很慢(与Java相比)? [英] Why is Go so slow (compared to Java)?

查看:972
本文介绍了为什么Go很慢(与Java相比)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们从计算机语言基准游戏中看到的那样:





用于执行的本地代码?

用于Go的不成熟编译器?或者,Go语言有一些内在问题?



编辑:

大多数答案否认Go语言的内在缓慢,声称问题存在于未成熟编译器。

因此,我已经做了一些自己的测试计算斐波纳契数:迭代算法运行在Go(freebsd,6g)中使用与C中相同的相同的速度(使用O3选项)。无效的递归运行在Go 2次中比在C中运行慢(使用-O3选项;与-O0相同)。但我还没有看到基准测试游戏中的10倍跌幅。

解决方案

6g和8g编译器并没有特别优化,所以他们生成的代码并不是特别快。他们被设计为自己运行速度很快,并生成可以运行的代码(有一些优化)。 gccgo 使用GCC现有的优化通行证,并可能与C进行更有意义的比较,但gccgo尚未完成功能。



基准数字几乎完全取决于实施质量。除了实现支持运行时支持语言功能的基准不真正需要的程度之外,它们与语言本身并无太大关系。在大多数编译语言中,一个足够聪明的编译器理论上可以删除不需要的东西,但是由于很少有真正的语言用户会编写不使用该功能的程序。如果没有完全移除它们(例如,在JIT编译的Java中预测虚拟调用目标),开始变得棘手。



FWIW,我自己的非常小的测试当我看看它时(基本上是一个整数加法循环),gccgo产生的代码在 gcc -O0 和<$ c之间的范围的最后端$ c> gcc -O2 对于等价的C. Go本质上并不慢,但编译器并没有做任何事情。对于10分钟以前的语言而言,这并不奇怪。


As we can see from The Computer Language Benchmarks Game:

  • Go is on average 10x slower than C
  • Go is 3x slower than Java !?

How can this be, bearing in mind that Go compiler produces native code for execution?
Immature compilers for Go? Or there is some intrinsic problem with the Go language?

EDIT:
Most answers deny intrinsic slowness of Go languge, claiming the problem resides in immature compilers.
Therefore I've made some own tests to calculate Fibonacci numbers: Iterative algorithm runs in Go (freebsd,6g) with the same speed as in C (with O3 option). The dull recursive one runs in Go 2 times slower than in C (with -O3 option; with -O0 - the same). But I haven't seen 10x fall as in the Benchmarks Game.

解决方案

The 6g and 8g compilers are not particularly optimising, so the code they produce isn't particularly fast.

They're designed to run fast themselves and produce code that's OK (there is a bit of optimisation). gccgo uses GCC's existing optimisation passes, and might provide a more pointful comparison with C, but gccgo isn't feature-complete yet.

Benchmark figures are almost entirely about quality of implementation. They don't have a huge amount to do with the language as such, except to the extent that the implementation spends runtime supporting language features that the benchmark doesn't really need. In most compiled languages a sufficiently clever compiler could in theory strip out what isn't needed, but there comes a point where you're rigging the demo, since very few real users of the language would write programs that didn't use that feature. Moving things out of the way without removing them entirely (e.g. predicting virtual call destinations in JIT-compiled Java) starts to get tricky.

FWIW, my own very trivial test with Go when I was taking a look at it (a loop of integer addition, basically), gccgo produced code towards the fast end of the range between gcc -O0 and gcc -O2 for equivalent C. Go isn't inherently slow, but the compilers don't do everything, yet. Hardly surprising for a language that's 10 minutes old.

这篇关于为什么Go很慢(与Java相比)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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