I tested this with python 3.5 in Debian Stretch.
I tried benchmark the "Avoiding dots" optimization.
As expected, the "Avoiding dots" optimization is really much faster.
Unexpected, timeit reports the slower code as the faster.
What is the reason?
$ time python3 -m timeit -s "s=''" "s.isalpha()"
10000000 loops, best of 3: 0.119 usec per loop
real 0m5.023s
user 0m4.922s
sys 0m0.012s
$ time python3 -m timeit -s "isalpha=str.isalpha;s=''" "isalpha(s)"
1000000 loops, best of 3: 0.212 usec per loop
real 0m0.937s
user 0m0.927s
sys 0m0.000s
2
Answers
Thank to Davis Herring's answer.
Let's make understand at more details:
From
python3 -m timeit -h
:Verify by calculate the details information:
All the
x loops -> y secs
time sum is used to determine the suitable loops number.Each items in the "raw time" line are single repeat timer result(the
-r
option determine the number of items in the "raw time" line).Almost all time is matched:
timeit
did 10 times as many iterations in the “slow” case. It adaptively tries more iterations to find a number that balances statistical quality and waiting time.