4 ms·
Python: def f(x): return x * x; v=0.0 for i in range(100000000): v = v + f(i) Execution time: 15 seconds --- JavaScript: function
by Aardwolf 4y ago
Python:
def f(x):
return x * x;
v=0.0
for i in range(100000000):
v = v + f(i)
Execution time: 15 seconds
---
JavaScript:
function f(x) { return x * x; }
var v = 0.0;
for(var i = 0; i < 100000000; i++) v += f(i);
Execution time: 0.5 seconds
---
So calling a function in a for loop in Python is still 30x slower than in JavaScript which is an as-dynamic language. Good to see a 24% increase, but a 3000% increase should still be possible.
I did this test in python 3.10, not python 3.11, but I assume if they did have a 30x speedup from the inlined python to python function calls this would have been mentioned, but the fastest speedup listed is 1.96x faster.
- nly 4y agoPresumably you're comparing a JavaScript JIT to interpreted Python (CPython)? Surely PyPy would be a fairer comparison
- Aardwolf 4y agoIndeed comparing with CPython, which the link is about. Indeed CPython is not JIT and PyPy is, but CPython is unfortunately usually what you get, comparing with PyPy would not be fairer since the goal is to speed up "vanilla" CPython. It's unfortunate to me that the default python interpreter, so widely used for scientific computational purposes, is so much slower than the JS interpreter you get in your web browser, and then we're being happy +24% benchmark results about this when 30x faster is known to be possible (e.g. with JIT).
- freediver 4y agoJust checked and pypy has the same speed as JS (instant). You are right that this is not what the most people use when working with Python and there is package incompatibility. I am curious but don't have the time to check would the same result be for a regex?
- Aardwolf 4y agoI didn't check myself, but in the link from the other comment regex is the one thing where python wins! https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Of course this is testing two native implementations of regex parsing, not the higher level scripting language handling.
- igouy 4y agoPCRE2 "wins" ;-)
- nly 4y agoRight but there's no "standard" JavaScript engine either. Benchmarking V8 against CPython and pointing out a totally expected 30x speed difference isn't insightful
- Aardwolf 4y agoI still have to disagree, it's not because both slow and fast JS engines exist (recent ones vs those from before around 2008), or both slow and fast python engines exist (CPython vs PyPy), that it's not ok to point out that the vanilla official python interpreter is much slower than possible. Why does one have to be ok with a totally expected speed difference? This is the official interpreter of a language, they give a slow tool by default, the official tool deserves scrutiny.
- igouy 4y agoPython: JavaScript: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- ris 4y ago> JavaScript which is an as-dynamic language It really is not. Python allows a huge amount of the object model to be overridden and introspected at any point. Javascript doesn't even allow operator overloading.
- lenkite 4y agoCan't Python just get a `const` keyword and CPython can then optimize accordingly ?
- frozenport 4y agoYou that like its a bad thing :-P
- pretext-1 4y agoTrue, but if I want a proper type system then JavaScript is not an option either.
- deleted 4y ago[deleted]