4 ms·
So, the usual caveats about writing code to match the languages strengths apply here. For example the fib function plays to a python weakness - function overhea
by kghose 15y ago
So, the usual caveats about writing code to match the languages strengths apply here. For example the fib function plays to a python weakness - function overhead. Rewriting the code to be a simple loop removes the inefficiency:
import time
def fib(n):
if n < 1:
return None
if n == 1:
return 0
if n == 2:
return 1
x0 = 0
x1 = 1
for m in range(2,n):
x = x0 + x1
x0 = x1
x1 = x
return x
if __name__=="__main__":
assert fib(21) == 6765
tmin = float('inf')
for i in xrange(5):
t = time.time()
f = fib(20)
t = time.time()-t
if t < tmin: tmin = t
print str(tmin*1000)
- StefanKarpinski 15y agoThat's quite true, but this micro-benchmark wasn't chosen to make Python look bad — it was chosen to test how good each language was at function calls. So using a loop defeats the point of the benchmark. Using double recursion to compute Fibonacci numbers is also stupid and could obviously be avoided in all languages. The story the micro-benchmark tells is that Julia's pretty good at function calls, but JavaScript is even better (and, of course, C/C++ is the gold standard). In general the V8 engine is really amazing. We had the advantage of being able to design the language to make the execution fast (with the constraints of all that we wanted to be able to do with it), but V8 makes a language that was in no way designed for performance and makes it blazingly fast. After I wrote the benchmark code for JavaScript and saw just how fast it was I had a moment of "should we be doing scientific computing in JavaScript?" Now wouldn't that be nuts?
- ViralBShah 15y agoYes, I pretty much had the same thought, but then I started thinking about multiple dispatch, calling C/Fortran libraries, polyhedral optimizations, and I realized that V8 developers may not have the same design targets in mind.
- pron 15y ago... and the JVM (HotSpot) is even better than V8.
- StefanKarpinski 15y agoI feel like V8 faces a harder problem and has solved it ingeniously. Not to knock HotSpot, which is excellent and keeps getting better — it just seems like making the JVM go fast is not as hard as making something like JavaScript go fast. It's a tough comparison; kind of apples to oranges.