This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: timeit makes code run faster?
Type: Stage: resolved
Components: Benchmarks Versions: Python 3.3
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: icedream91, r.david.murray, sbt
Priority: normal Keywords:

Created on 2013-06-18 09:25 by icedream91, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
23.py icedream91, 2013-06-18 09:41 By default, the code will be tested by timeit function. If you want to test the code without using timeit function, just comment line 5 and line 68-72.
Messages (5)
msg191390 - (view) Author: (icedream91) Date: 2013-06-18 09:25
I used Python 3.3.2 to try this problem: http://projecteuler.net/problem=23 , and I got a correct answer.

When I wanted to check how long it took, I found something strange:
When I ran 23.py directly, it showed that it took about 13s. But if I use timeit module, it showed that it only took about 9s! I have tried these for some times, in both Ubuntu 12.10 and Windows 8, I don't understand why timeit will make the same code run faster.


Thanks.
msg191391 - (view) Author: (icedream91) Date: 2013-06-18 09:41
I used Python 3.3.2 to try this problem: http://projecteuler.net/problem=23 , and I got a correct answer.

When I wanted to check how long it took, I found something strange:
When I ran 23.py directly, it showed that it took about 13s. But if I use timeit module, it showed that it only took about 9s! I have tried these for some times, in both Ubuntu 12.10 and Windows 8, I don't understand why timeit will make the same code run faster.


Thanks.
msg191393 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2013-06-18 10:07
I think if you use timeit then the code is wrapped inside a function before it is compiled.  This means that your code can mostly use faster local lookups rather than global lookups.
msg191397 - (view) Author: (icedream91) Date: 2013-06-18 11:42
I did some tests, Richard Oudkerk (sbt) is right. Thanks a lot.
msg191401 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-06-18 12:22
By the way, this kind of question is more suited to the python-list mailing list (where you will in the general case get a faster answer anyway :)
History
Date User Action Args
2022-04-11 14:57:47adminsetgithub: 62452
2013-06-18 13:06:55sbtsetstage: resolved
2013-06-18 12:59:32sbtsetstage: resolved -> (no value)
2013-06-18 12:22:50r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg191401

stage: resolved
2013-06-18 11:42:25icedream91setmessages: + msg191397
2013-06-18 10:07:02sbtsetnosy: + sbt
messages: + msg191393
2013-06-18 09:41:24icedream91setfiles: + 23.py

messages: + msg191391
2013-06-18 09:38:06icedream91setfiles: - 23.py
2013-06-18 09:25:07icedream91create