V8 performance can beat C sometimes
benchmarksgame.alioth.debian.org
benchmarksgame.alioth.debian.org
Please, let me know if you find the opposite.
[1] http://blog.chromium.org/2009/02/irregexp-google-chromes-new...
I was thinking: but V8 doesn't have byte code.
Then opened the link:
"After optimization we generate native machine code which uses backtracking to try different alternatives."
http://v8.googlecode.com/svn/trunk/src/interpreter-irregexp....
Skimming the code it does look like they are interpreting byte code from Irregexp but I would need to read further to say it for sure.
1 - http://code.google.com/p/v8/source/browse/trunk/src/bytecode...
2 - http://code.google.com/p/v8/source/browse/trunk/src/x64/rege...
But it does show how good V8 is at optimizing out the JS part of JS when it's irrelevant to the computation, and it shows how well-optimized the regexps in V8 are.
None of this is terribly surprising given V8's use case, but only a few years back this would have been a ridiculous result. I enjoyed taking a minute to reflect on that.
The point is that a regular expression compiled by V8 (which is not written in JavaScript) is faster than a regular expression as compiled by some particular C compiler. Of course a C compiler could possibly be written to understand calls to a standard regexp API and compile those to binary.
(The C and C++ thing is just adding confusion here)
This is similar to the advantage of abstraction layers in general, although there's a cost if you create abstraction layers for things no-one much cares about.
JS: http://benchmarksgame.alioth.debian.org/u64/program.php?test...
C: http://benchmarksgame.alioth.debian.org/u64/program.php?test...
I'll go out on a limb here and claim that this C program is not the shortest possible equivalent to the JS version.
http://benchmarksgame.alioth.debian.org/u64/program.php?test...
http://benchmarksgame.alioth.debian.org/u64/program.php?test...
This is V8 calling a regexp library written in C++. C just happens to be using a different library that is slower.
But it also beat the C++ implementation though.
It would be interesting, however, to compare the performance here to that of the "grep" command on the same expressions. I'd be surprised at anything short of a total smackdown of Javascript V8.
I would expect an even bigger smackdown if one invoked GPUs:
http://bkase.github.io/CUDA-grep/finalreport.html
Even more interesting would be an OpenCL implementation that did exactly what V8 is doing to the regexes (Why? Because it can).
And total aside, this is why I don't participate in TopCoder competitions - they never let you have any real fun.
Hardly surprising.
http://benchmarksgame.alioth.debian.org/u64/program.php?test...
(And the program is IMHO clearer than the C implementation)
Disclaimer: I had anything to do with this, I just thought it was interesting enough to post it.