Created
December 16, 2015 18:42
-
-
Save hoov/819c4f6ee4e415cefa23 to your computer and use it in GitHub Desktop.
Weird...
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
#!/usr/bin/env python | |
import time | |
from contextlib import contextmanager | |
from itertools import repeat | |
def way_one(): | |
count = 0 | |
for x in repeat(None, 10000): | |
count += 1 | |
return count | |
def way_two(): | |
for count, x in enumerate(repeat(None, 10000)): | |
pass | |
return count + 1 | |
@contextmanager | |
def timed(description): | |
start = time.time() | |
yield | |
end = time.time() | |
print("%r took %.3f ms" % (description, (end - start) * 1000.0)) | |
with timed("increment"): | |
for _ in repeat(None, 1000): | |
way_one() | |
with timed("enumerate"): | |
for _ in repeat(None, 1000): | |
way_two() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'increment' took 194.088 ms
'enumerate' took 174.236 ms