Created
May 22, 2014 07:07
-
-
Save vporoshok/abed2d90ceb9a90a7a4a to your computer and use it in GitHub Desktop.
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
#!/bin/env python | |
# -*- coding: utf-8 -*- | |
from timeit import timeit | |
class Fib: | |
def __init__(self, end): | |
self.a = 0 | |
self.b = 1 | |
self.cur = 0 | |
self.end = end | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.cur == self.end: | |
raise StopIteration | |
self.cur += 1 | |
self.a, self.b = self.b, self.a + self.b | |
return self.a | |
exception = '''\ | |
try: | |
d[1] | |
except KeyError: | |
1 | |
''' | |
bad_key = 'd={-x: x for x in xrange(1000000)}' | |
good_key = 'd={x: x for x in xrange(1000000)}' | |
def main(): | |
print timeit('d.get(1, 1)', bad_key, number=1000000) | |
print timeit('d.get(1, 1)', good_key, number=1000000) | |
print timeit(exception, bad_key, number=1000000) | |
print timeit(exception, good_key, number=1000000) | |
print timeit('d[1] if 1 in d else 1', bad_key, number=1000000) | |
print timeit('d[1] if 1 in d else 1', good_key, number=1000000) | |
for x in Fib(5): | |
print x | |
print numberxt(Fib(3)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment