200 points · by pgbovine · 15y ago · 134 comments
"We find that in regards to performance, C++ wins out by a large margin. However, 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.
Scala concise notation and powerful language features allowed for the best optimization of code complexity. The Java version was probably the simplest to implement, but the hardest to analyze for performance. Specifically the effects around garbage collection were complicated and very hard to tune. Since Scala runs on the JVM, it has the same issues.
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."
[citation needed]
I am a fan of C++, so I liked the first part (that C++ wins, which is not really surprising). However it was not clear if the sophistication they talk about is regarding the Google's internal data-structures or the list of optimizations listed in Section VI-D? Many of these optimizations are not big surprises to a C++ programmer (where possible use hash_map, vector instead of list, initialize data structure outside of loop and try to reuse, and so on).
Overall, I do greatly appreciate the effort and their sharing the results.
In fact, looking at the list of optimizations on page 9, the only one that involved a low-level understanding of the machine and resulted in a significant improvement was (unless I missed something) the use of InlinedVector (and this is only barely such a case - lots of C++ codebases have a similar class).
By the way, std::list::size() is defined to be O(1) (see http://www.kuzbass.ru:8086/docs/isocpp/lib-containers.html#l...). While it's possible, of course, to define a linked-list where the size operation is O(n), that's not an implementation of std::list. Hence, there was no difference when std::list::empty() was used - although I agree it's better to use it, since it's more meaningful.
Scala without any optimization is still better than Java with all the ninja-skills black magic applied. Scala optimized is pretty good - "just" 2.5x slower than C++
Does that mean the java version would have been just as fast? It seems in general though that Java is just as fast as C++ unless you are an ultimate C++ expert.
Note that Jeremy deliberately refused to optimize the code further, many of the C++ optimizations would apply to the Java version as well
It sounds like Jermey didn't spend much time tuning the Java version either.
They note in the paper this distinction, but it should be marked in the table, because I'm sure 99% of people don't see that. Sloppy!
"... the benchmark also used a HashMap<Type, Integer> when it should have just stored a primitive int in Type; it also used a LinkedList where it should have used an ArrayDeque."
http://jeremymanson.blogspot.com/2011/06/scala-java-shootout...
Pentium IV - really? Let's see - we've had Core, Core 2, Nehalem and Sandy Bridge since then, not to mention 3 in-between process shrink versions of the same architecture.
Perhaps we could pass the hat around so that Google could afford a Sandy Bridge workstation and discover what these interesting results look like on an architecture that dates from some point in this decade - or at least, at some point in the previous decade.
This stuff actually, really, truly makes a difference. And not necessarily in favor or against any particular one of these languages...
On the other hand, the processors of 2000 are quite a bit different to the processors of 2011, and anyone who thinks that they can prove that this is irrelevant to the experiment in question by pure reasoning (rather than experimentation) is a moron, plain and simple.
Having tracked individual embodiments of algorithms over architectures from the P4 to Sandy Bridge, I can assure you that tradeoffs between particular approaches on an given problem vary wildy based on changes in microarchitecture.
Overall, there's a steady improvement and perhaps these languages are all gaining exactly the same (despite a substantial improvement in effective ILP from P4 to Core 2)... but I don't know that for sure and neither do you, despite your sophomoric self-assurance.
If only they could do the experiments on a processor from the last 5-6 years...naturally someone else could do the work for them, but why bother?
This is much more interesting considering our current reality.
I once gained 10% speed up on two cores by changing just two lines of our optimization tool written on Haskell.
You're right that there may not be much improvement, but I've seen the google dense_hash_map do better sometimes than the basic SGI hash_map.
Edit: the code repo has the unoptimized versions. Oh well, at least I can get the satisfaction of seeing the 30% myself :)
afaict Yes, they left the internal version out completely (besides the changelog).
Notice C++ Dbg has the same wc -l as C++ Opt.
Here is the output of sloccount:
SLOC Directory SLOC-by-Language (Sorted)
595 java_pro java=595
591 java java=591
488 cpp cpp=488
328 python python=328
0 go (none)
0 go_pro (none)
0 scala (none)
0 scala_pro (none)
generated using David A. Wheeler's 'SLOCCount'.
Compare it to the paper. Benchmark wc -l
C++ Dbg/Opt 850
Java 1068
Java Pro 1240
Scala 658
Scala Pro 297
Go 902
Go Pro 786 Benchmark GZ Bytes Factor (wc -l Factor from paper)
Java Pro 5198 1.65x 1.9x
Java 4403 1.40x 1.6x
C++ 4229 1.34x 1.3x
Go 3768 1.20x 1.4x
Go Pro 3259 1.03x 1.2x
Scala 3138 ==== ===
Python 2755 0.87x ????
Scala Pro 1929 0.61x 0.5x Bench files blank comment code
cpp 4 145 236 501
go 5 153 239 546
go_pro 5 132 233 457
java 8 151 352 605
java_pro 8 157 351 772
python 2 110 204 334
scala 2 104 224 384
scala_pro 2 59 62 230Challenge to the pythonistas: rewrite the python version so it uses the C++ code under the hood using scipy weave
Edit:
The Python version can be found at http://code.google.com/p/multi-language-bench/source/browse/...
http://code.google.com/p/multi-language-bench/
I confess that I do not know a lot about Scala, but it looks like some of the functional aspects of the language allow for some savings in lines of code.
If I had the time right now, I doubt that it would be too difficult to shrink the python version a bit. Just taking a glance at it, I don't see a single list comprehension throughout their code. I am sure there are some other language features that could probably have been better leveraged as well.
Just out of curiosity, where did you get the 626 loc and the 297 loc from - I tried looking but I can't find it anywhere. Though that could just be a product of my lack of sleep right now.
I suspect the author gives it out as a reference implementation.
http://code.google.com/p/multi-language-bench/source/browse/...
No, there are no ctype libraries in there.
Of course, in most cases, you're right. Given an infinite pool of very good programmers and infinite time, C will almost always be faster than most other languages. But in reality, you have a very limited of maybe not so good programmers, and the project needed to be done yesterday :)
http://www.elis.ugent.be/JavaStats
http://www.bestinclass.dk/index.clj/2010/02/benchmarking-jvm...
http://isthisclojure.blogspot.com/2011/02/benchmark-clojurej...
https://docs.google.com/present/view?id=0AS8emH3-FLt3ZGRtbWJ...
Examples of benchmarks gone awry:
http://stackoverflow.com/questions/6146182/why-is-my-scala-c...
http://groups.google.com/group/scala-language/browse_thread/...
http://download.oracle.com/docs/cd/E15289_01/doc.40/e15060/t...
A less popular language - or one that is usually used for less-critical code - will suffer.
C++ wasn't always so fast. See Kernighan and Pike, "The Practice of Programming" for a simple example where Java surprisingly beats C++.
I would love to use Digital Mars D or Scala for scientific applications if they had compilers/runtimes as good as C++.
Perhaps one day the LLVM backend would be useful as a universal backend for many less popular languages.
Looks like C++ is still one of the performance kings among current programming languages. Can't say I enjoy using it though.
It also looks like Go and Scala have a long way to go on compiler optimization. Not surprising, since they're young languages compared to C++ and Java, and it's a decent amount of work to optimize the higher-order constructs they provide.
It'll take a pretty significant shift to change this. Something like 100+ core cpus each running slow, might be such a shift.
Being surprised that C++ is faster than newer languages which provide all types of abstraction is like being surprise assembly is faster than C++.
All the C++ hate is blown way out of proportion. C++ is a great tool when you need performance. Tuning C++ isn't that difficult either. Contrary to what a commenter said above, steering clear of C++ at all costs is not what you should be doing. You should know when and how to use C++. It's useful more often than many people here seem to think. Especially if you develop for mobile devices.
edit: I just got to section VI of the paper, which does some gc tuning. In my playing, I also found about a 20% speedup by changing the HavlakLoopFinder.UnionFindMode class to being a statuc class.
Although you shouldn't necessarily think "Oh, so that's how that's done in this language."
http://code.google.com/p/multi-language-bench/source/browse/...
Can't wait to see Modula-3's numbers…
"they"? The space monsters who run the universe?
On the other hand, it does compile fast.
for(std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); it++)
you can just do:
for(std::string x : vec)
So awesome!
for (auto &x : vec) { }
Submissions must be in English and at most 12 pages total length in the standard ACM SIGPLAN two-column conference format (10pt).
Nearly all academic papers in computer science and engineering look like this. Not to say they've converged on an optimal format, but it wasn't exactly a creative decision on the part of these authors.