Scala:在性能与开发效率中寻找平衡
正如《黑客与画家》作者所说,选择正确的语言对于项目不仅重要,甚至是举足轻重的。至少我曾经在学习除C++意外的新语言的时候,就犹豫过:
在Scala Days 2011,Google的工程师给出了具有参考力的报告。
-
<< Scala Days 2011(2011.06.02 -- 2011.06..3)
http://days2011.scala-lang.org/
Scala的同志们聚在一起开会了,有不少好的话题,我最感兴趣的就是Scala&LLVM和Scala V.S. C++/Go/Java了。
<< Loop Recognition in C++/Java/Go/Scala
有Google好事人民做的Scala/C++/Java/Go的比较,并在Scala Days发表论文,Piaoger之前就留意到这篇论文,正等着它的发表。
下面仅仅摘抄了比较结果:
Code Size in [Lines of Code]: Scala < C++/Go < Java
Scala需要代码行最少,可以达到是其他几种语言的一半甚至更多,有Value。
本以为C++代码难写,比起Java来,还是可以少些活儿。
Compilation Times in [Secs]: GO < Java/C++/Scala fsc << Scala Scalac
Go语言编译起来比较快,而Scala就差强人意了。
Binary and JAR File Sizes in [Byte]: Java < C++/Scala << Go
Go的文件也忒大了,比C++ Debug版本的文件都要大一倍去了,而Scala与C++不分伯仲。
Memory Footprint: C++ < Scala < Go/Java
C++程序启动时间确实要短些,有感觉的。
Run-time measurements: C++ > Scala > Go > Java
总的来说,C++在性能还是高出一筹的,而Scala有望在性能与开发效率上获得平衡;
Go语言还年轻,还有潜力;至于Java,确实不如Scala来得生猛。
原文与源代码: http://code.google.com/p/multi-language-bench
文中还提到了这几种语言的一些优化方法,可以一看。
----
Piaoger后记:
这篇文章的推出,C++社区炸开了锅,毕竟从很多文章和访谈中,N多niuren对C++很是不感冒,这个报告让N多人涨了一口气:
在Comment中,有一位仁兄的评价颇为中肯。在这位仁兄的Comment中,提到了两个比较,发现LISP语言居然是Performance/Efficience结合的很棒的语言。
---------------------------------------------------------
Google has released a research paper [scala-lang.org] (PDF) that suggests C++ is the best-performing language [computing.co.uk] on the market.
No, they didn't. They compared four languages (C++, Go, Java and Scala) using a single algorithm, and two implementations (initial and improved) per language. Out of those, the optimized C++ turned out to be the fastest and the least memory hungry, whereas the improved Scala version used the least source code, and the improved Go version compiled the fastest.
None of this allows generalization to "best-performing programming language on the market".
It's not for everyone, though. They write, '...it also required the most extensive tuning efforts, many of which were done at a level of sophistication that would not be available to the average programmer.'
This is a very important point. If you are Google, you probably have developers who can do this kind of tuning, and you will probably benefit from it (the developer effort is expensive, but inefficient software may well be more expensive at Google's scale).
In general, though, what you want to consider is not only the best performance that has been produced by the world on a single problem, but also the performance on different problems, the variation in performance between implementations, the average performance, and the development time.
In 2000, Erann Gatt (now Ron Garret) published a paper [flownet.com] (PDF) that showed the results of comparing 16 implementations written by 14 programmers, in C or C++ (lumped together), Java, and Common Lisp or Scheme (lumped together). These results show that the fastest programs were written in C or C++, Lisp produced the fastest programs on average, and offered the least variation in performance. Lisp also offered the shortest development time on average.
Of course, this is old data. If anyone has performed a similar study more recently, or including different problems to be solved rather than a single one, I would be very interested.
Meanwhile, the Computer Language Benchmarks Game [debian.org] compares many language implementations across several different tasks, with multiple programs for each task, and shows that the results differ depending on exactly how you measure.
Apparently, if you want the fastest programs, you should go with C, C++, Ada, ATS (Fortran, Common Lisp, and Python also produced fast programs, but weren't as good on average). If you want short programs (which may be expected to correlate with short development time), you might want to go with Ruby, Python, Perl, Lua, or JavaScript. If you want short development time, but also reasonable performance, then Go, Scala, or Haskell may be good choices (or you could go the time-tested route of writing what you can in rapid development languages, and the parts that need to be fast in high-performance languages).
---------------------------------------------------------
还有这个地方,也是一阵闹腾:
http://www.reddit.com/r/programming/comments/hqkwk/google_paper_com