4

I am trying to understand timeit module for PYTHON.

I have a very simple function, it just prints "hi" I then used timeit module to print the time elapsed for this function, but there is one output "0.0010001659393310547" other than "0.0"

Why does this happen? How to make the output stable, which means only has one number forever? Otherwise not sure if I apply this to my project, will it give me the correct anwser...

my code as below:

>>> def hi():
...     print "hi"
...
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0010001659393310547
>>> timeit.timeit(hello,number=6)
hi
hi
hi
hi
hi
hi
0.0
LinconFive
  • 1,718
  • 1
  • 19
  • 24
  • althought I think it is because of processing or memory when that one isn't exactly 0.0, you could add the code for review and others will provide an answer if possible – davejal Nov 19 '15 at 02:30
  • 2
    It is different because time is not infinitely precise (try measuring the length of your arm three times, and see if you get the exactly same result), and because the function's execution time is not constant (e.g. the computer might be busy with something else). Whenever you measure anything, measure it several times, then average - that's how all science is done. – Amadan Nov 19 '15 at 02:30
  • @Amadan I think i got your idea, which is based on science measurement. But can you refer me to any link that describes how the interpreter(for PYTHON) work with the real time? in terms of memory, if every-time the interpreter uses same memory, will the processing take same amount of time? – LinconFive Nov 19 '15 at 18:54

0 Answers0