Created
July 13, 2014 22:27
-
-
Save wrobstory/1200cda56a740486701b to your computer and use it in GitHub Desktop.
Python 2.6 set() vs locals()['_[1]']
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.6.9 |Continuum Analytics, Inc.| (unknown, Jan 10 2014, 13:33:57) | |
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import timeit | |
>>> from math import sqrt | |
>>> | |
>>> n = 100 | |
>>> sqrt_n = int(sqrt(n)) | |
>>> | |
>>> def with_set(): | |
... primes = [j for i in range(2,sqrt_n) for j in range(i*2, n, i)] | |
... return set(primes) | |
... | |
>>> def with_locals(): | |
... return [j for i in range(2,sqrt_n) for j in range(i*2, n, i) | |
... if j not in locals()['_[1]']] | |
... | |
>>> timeit.timeit(with_set, number=100000) | |
1.5858581066131592 | |
>>> timeit.timeit(with_locals, number=100000) | |
13.023312091827393 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment