Created
May 9, 2012 17:32
-
-
Save benhoyt/2647005 to your computer and use it in GitHub Desktop.
Speed of list comprehension vs for loop with append
This file contains 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
# On my Windows 7 64-bit machine running CPython 2.6 | |
C:\>python -m timeit -s "lst = [1] * 100" "out = [x for x in lst if x]" | |
100000 loops, best of 3: 6.55 usec per loop | |
C:\>python -m timeit -s "lst = [1] * 100" "out = []" "for x in lst:" " if x:" " out.append(x)" | |
100000 loops, best of 3: 14.7 usec per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment