英文:
Performance of Google's Go?
问题
有人使用过谷歌的Go语言吗?我想知道它在数学性能方面(例如flops)与其他带有垃圾回收器的语言(如Java或.NET)相比如何?
有人调查过这个吗?
英文:
So has anyone used Google's Go? I was wondering how the mathematical performance (e.g. flops) is compared to other languages with a garbage collector... like Java or .NET?
Has anyone investigated this?
答案1
得分: 82
理论性能:纯Go程序的理论性能介于C/C++和Java之间。这假设有一个先进的优化编译器,并且假设程序员充分利用语言的所有特性(无论是C、C++、Java还是Go),并重构代码以适应编程语言。
实际性能(截至2011年7月):标准的Go编译器(5g/6g/8g)目前无法为高性能数值代码生成高效的指令流,因此性能将低于C/C++或Java。这有多个原因:每个函数调用都有几个额外指令的开销(与C/C++或Java相比),没有函数内联,平均质量的寄存器分配,平均质量的垃圾收集器,有限的能力来消除边界检查,无法从Go访问矢量指令,编译器不支持32位x86 CPU上的SSE2等。
底线:作为经验法则,预计纯Go实现的数值代码在5g/6g/8g编译下的性能将比C/C++或Java低2倍。预计未来性能会有所提高。
实际性能(2013年9月):与2011年7月的旧版Go相比,Go 1.1.2能够生成更高效的数值代码,但它们仍然比C/C++和Java运行稍慢。编译器即使在32位x86 CPU上也利用了SSE2指令,这导致32位数值代码运行速度更快,很可能是由于更好的寄存器分配。编译器现在实现了函数内联和逃逸分析。垃圾收集器也得到了改进,但仍然不如Java的垃圾收集器先进。仍然没有支持从Go访问矢量指令。
底线:Go与C/C++和Java在数值计算中的性能差距似乎足够小,除非竞争实现使用矢量指令,否则Go可以作为C/C++和Java的替代品。
英文:
Theoretical performance: The theoretical performance of pure Go programs is somewhere between C/C++ and Java. This assumes an advanced optimizing compiler and it also assumes the programmer takes advantage of all features of the language (be it C, C++, Java or Go) and refactors the code to fit the programming language.
Practical performance (as of July 2011): The standard Go compiler (5g/6g/8g) is currently unable to generate efficient instruction streams for high-performance numerical codes, so the performance will be lower than C/C++ or Java. There are multiple reasons for this: each function call has an overhead of a couple of additional instructions (compared to C/C++ or Java), no function inlining, average-quality register allocation, average-quality garbage collector, limited ability to erase bound checks, no access to vector instructions from Go, compiler has no support for SSE2 on 32-bit x86 CPUs, etc.
Bottom line: As a rule of thumb, expect the performance of numerical codes implemented in pure Go, compiled by 5g/6g/8g, to be 2 times lower than C/C++ or Java. Expect the performance to get better in the future.
Practical performance (September 2013): Compared to older Go from July 2011, Go 1.1.2 is capable of generating more efficient numerical codes but they remain to run slightly slower than C/C++ and Java. The compiler utilizes SSE2 instructions even on 32-bit x86 CPUs which causes 32-bit numerical codes to run much faster, most likely thanks to better register allocation. The compiler now implements function inlining and escape analysis. The garbage collector has also been improved but it remains to be less advanced than Java's garbage collector. There is still no support for accessing vector instructions from Go.
Bottom line: The performance gap seems sufficiently small for Go to be an alternative to C/C++ and Java in numerical computing, unless the competing implementation is using vector instructions.
答案2
得分: 35
Go math package主要是用汇编语言编写的,以提高性能。
基准测试通常不可靠,并且容易被解释。例如,Robert Hundt的论文Loop Recognition in C++/Java/Go/Scala似乎有缺陷。Go博客上的Profiling Go Programs一文对Hundt的说法进行了剖析。
英文:
The Go math package is largely written in assembler for performance.
Benchmarks are often unreliable and are subject to interpretation. For example, Robert Hundt's paper Loop Recognition in C++/Java/Go/Scala looks flawed. The Go blog post on Profiling Go Programs dissects Hundt's claims.
答案3
得分: 15
你实际上问了几个不同的问题。首先,Go的数学性能将与其他任何语言一样快。任何编译成本地代码的语言(甚至包括像.NET这样的JIT语言)在原始数学方面都会表现得非常出色-与机器的速度一样快。简单的数学运算非常容易编译成零开销的形式。这是编译(包括JIT)语言在解释性语言上具有优势的领域。
你提出的另一个问题是垃圾回收。如果你谈论的是大量的数学计算,这在某种程度上是一个次要问题。这并不是说GC不会影响性能-实际上它会影响很多。但是对于紧密循环的常见解决方案是避免或最小化GC扫描。如果你正在进行紧密循环,这通常非常简单-你只需重复使用旧变量而不是不断分配和丢弃它们。这可以将代码的速度提高几个数量级。
至于GC的实现本身-Go和.NET都使用标记-清除垃圾回收。微软已经将大量精力和工程投入到他们的GC引擎中,我不得不认为考虑到所有情况,它相当不错。Go的GC引擎还在不断改进中,虽然它的架构并不比.NET的架构慢,但Golang的人坚持认为它还需要一些工作。Go的规范不允许析构函数这一事实在加快速度方面起到了很大作用,这可能是为什么它看起来不那么慢的原因。
最后,根据我的个人经验,我发现Go非常快。我编写了非常简单易懂的程序,在我的基准测试中,它们与一些长期存在且备受尊敬的开源项目中高度优化的C代码相媲美,这些项目以性能为傲。
问题在于,并非所有的Go代码都是高效的,就像并非所有的C代码都是高效的一样。你必须正确构建它,这通常意味着与其他语言不同的做法。这里多次提到的性能分析博文就是一个很好的例子。
英文:
You're actually asking several different questions. First of all, Go's math performance is going to be about as fast as anything else. Any language that compiles down to native code (which arguably includes even JIT languages like .NET) is going to perform extremely well at raw math -- as fast as the machine can go. Simple math operations are very easy to compile into a zero-overhead form. This is the area where compiled (including JIT) languages have a advantage over interpreted ones.
The other question you asked was about garbage collection. This is, to a certain extent, a bit of a side issue if you're talking about heavy math. That's not to say that GC doesn't impact performance -- actually it impacts quite a bit. But the common solution for tight loops is to avoid or minimize GC sweeps. This is often quite simple if you're doing a tight loop -- you just re-use your old variables instead of constantly allocating and discarding them. This can speed your code by several orders of magnitude.
As for the GC implementations themselves -- Go and .NET both use mark-and-sweep garbage collection. Microsoft has put a lot of focus and engineering into their GC engine, and I'm obliged to think that it's quite good all things considered. Go's GC engine is a work in progress, and while it doesn't feel any slower than .NET's architecture, the Golang folks insist that it needs some work. The fact that Go's specification disallows destructors goes a long way in speeding things up, which may be why it doesn't seem that slow.
Finally, in my own anecdotal experience, I've found Go to be extremely fast. I've written very simple and easy programs that have stood up in my own benchmarks against highly-optimized C code from some long-standing and well-respected open source projects that pride themselves on performance.
The catch is that not all Go code is going to be efficient, just like not all C code is efficient. You've got to build it correctly, which often means doing things differently than what you're used to from other languages. The profiling blog post mentioned here several times is a good example of that.
答案4
得分: 2
谷歌进行了一项研究,比较了Go与其他一些流行的语言(C++,Java,Scala)。他们得出结论,Go在性能方面不如其他语言强大:
https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf
以下是关于Go的结论引用:
> Go提供了有趣的语言特性,也允许使用简洁和标准化的表示法。该语言的编译器仍然不够成熟,这反映在性能和二进制文件大小上。
英文:
Google did a study comparing Go to some other popular languages (C++, Java, Scala). They concluded it was not as strong performance-wise:
https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf
Quote from the Conclusion, about Go:
> Go offers interesting language features, which also allow for a concise and standardized notation. The compilers for this language are still immature, which reflects in both performance and binary sizes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论