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
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://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
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
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×).Edit: SO answer explaining the PyPy JIT: https://stackoverflow.com/questions/37377787/what-kind-of-ji...
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. Iteration 0 took 0.6579999923706055s
Iteration 1 took 0.6540000438690186s
Iteration 2 took 0.6460001468658447s
(I noticed the code from hastebin was an old version, I forgot to swap the starttime-time.time() in the version I posted. Not that it matters much, just change the sign.)Running it on OP's website again just to be sure, it now takes 4.9 seconds instead of 5.25, so there is some variance there, but this is still a big difference. This is Firefox as well (see another comment in this subthread), I don't have Chromium installed.
Besides, if calculations are important, you would use numpy.
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