Why is pypy clock () so imprecise?

I tried using some of my codes and noticed some really strange results with pypy. Take a look at this example:

from time import clock
from random import randint

times = []
for _ in range(10):
  start = clock()
  sum(i * i for i in range(randint(1000000, 2000000))) # waste time
  end = clock()
  times.append(int((end - start) * 1000000))

for t in times:
  print(t, "µs")

      

Running with python3

, there are a bunch of random results as expected:

506246 µs
461735 µs
403287 µs
472049 µs
651156 µs
609496 µs
467181 µs
633187 µs
562603 µs
744360 µs

      

Running with pypy, it seems like everything is only accurate to the third of a second:

43332 µs
36667 µs
30000 µs
23332 µs
19999 µs
23334 µs
26665 µs
20000 µs
36667 µs
26666 µs

      

Why is this?

+3


source to share


2 answers


The answer to the answer is correct in theory, but it really is a mistake. Please open a bug in the PyPy error controller stating exactly which platform and version of PyPy you are using (I seem to have got it right on Linux and John on Windows).



+1


source


From Python documentation :

The precision, and in fact the very definition of the "CPU time" value, depends on the C function of the same name.



The accuracy will differ for different implementations. In this case, pypy seems to use a timer with a 1/30 second period, so the accuracy will never be better.

+1


source







All Articles