20 ms·
The speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2
by lucb1e 8y ago
The speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2, 14.5 million loops in pypy, and 0.61 million loops in the browser.
My code basically calls int(time.time()) until its value changes, doing i+=1 for every time it did not change (n=10 for every platform (python2/3/pypy/browser)). My browser is Firefox 61, full code here: https://hastebin.com/turenofise.py https://hastebin.com/turenofise.py
- tim333 8y agoThough trying to add the numbers for 1 to a million in a loop javascript was about 10 - 20 times faster when I tried it.
- deleted 8y ago[deleted]
- sametmax 8y agoYou would use sum(range()) in a Python though. Remember that many things you do manually in JS have a better, automatic and faster way in the stlib. Besides, if calculations are important, you would use numpy.
- tim333 8y agoAh yes. sum(range()) in Python vs for(i=1...) in js was much closer, about 20% quicker in javascript.
- gengkev 8y agoWon't this calculation overflow to +Infinity in JavaScript after a while? On the other hand, Python will try to compute the actual result using arbitrary precision integers, which is that's slower.
- deleted 8y ago[deleted]
- alexchamberlain 8y agoI wonder if the time.time is the slow part there? Maybe worth doing a busy look for say 5 million iterations, timing that then doing the calculation for loops per second.
- lucb1e 8y agoI'm sure it is a thousand times slower than i++, but it should be the same code on each platform. But you made me think: indeed, the underlying call could be slower from JS whereas from pypy/python{2,3} it's fast (or vice versa). So I just tested a version where it checks how long ten million iterations take (n=3). Code here: https://hastebin.com/uyuhecabek.py https://hastebin.com/uyuhecabek.py browser/pypy 5.25 seconds python 3.6.6 2.16 seconds python 3.7.0 2.08 seconds python 2.7.15 1.42 seconds pypy 6.0.0 0.01 seconds Since there is no syscall to be made, pypy can do this in compiled code and is blazingly fast (I bet C/assembly isn't much faster there), and the rest seems comparable to the previous estimate: the browser version is 2.25× slower compared to python3 (now 2.43×) and 3.33× slower compared to python2 (now 3.70×).
- alexchamberlain 8y agoI'm surprised it's that different; in the optimum case, it feels like a several layers of JITing interpreter should get to the same performance in the end. Might not be enough to trigger it I guess.
- chmod775 8y agoI can't see the code because I'm on mobile, but if the JIT is function based and you're only calling the benchmarked function once it may not have it optimized for your first call. It certainly can't replace the instructions mid-loop. Edit: SO answer explaining the PyPy JIT: https://stackoverflow.com/questions/37377787/what-kind-of-jit-compiler-is-used-by-pypy https://stackoverflow.com/questions/37377787/what-kind-of-ji...
- aroberge 8y agoTested on Brython (with Firefox) - http://www.brython.info/tests/editor.html?lang=en http://www.brython.info/tests/editor.html?lang=en Iteration 0 took -0.9660000801086426s Iteration 1 took -0.9519999027252197s Iteration 2 took -0.9499998092651367s It is about 10 times slower with Chrome on my computer.
- philipov 8y agoWhich version of python 3 did you test with? I expected 3.6+ to be faster than python 2.7
- lerax 8y agoint in python2 is a more simple structure (fixed precision for non-long integers) than int of python3 (arbitrary precision)
- lucb1e 8y agoPython 3.6.6 from Debian Buster in a VirtualBox VM on a Windows 10 host. Testing with 3.7.0, the result is no different. Python 3.5 is no longer available in the repositories to test whether that is indeed slower.
- jwilk 8y agoThis pastebin requires JS... Here's the code: import time a=-1 j = 0 i=0 while j < 10: if a!=int(time.time()): print(i) a = int(time.time()) i=0 j += 1 i += 1
- asmosoinio 8y agoThere's some pypy.js vs cpython benchmarks at http://arewepythonyet.com/ http://arewepythonyet.com/ - those look very good as well.
- aisofteng 8y agoThe print will slow everything down as well.
- shuzchen 8y agoFetching time in the browser isn't accurate because a lot of trackers exploit timing in order to fingerprint users. A lot of browsers now purposefully reduce the precision of the clock. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Reduced_time_precision https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... https://johnresig.com/blog/accuracy-of-javascript-time/ https://johnresig.com/blog/accuracy-of-javascript-time/
- lucb1e 8y agoOh, how convenient. Sometimes I wonder if we shouldn't find a way to be able to run trusted applications instead of making everyone's life difficult all the time, and that's speaking as a security consultant, not even a developer... but yeah I don't see a good way to make that happen.
- brazzledazzle 8y agoThis reminds me a bit of ActiveX.
- userbinator 8y agoThe whole Meltdown/Spectre thing also contributed greatly to reducing timer precision.