Last active
August 29, 2015 14:11
-
-
Save gvx/272ef41bdd57dc1f2758 to your computer and use it in GitHub Desktop.
named tuple performance
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
$ py3.3 time_them.py | |
default implementation: | |
2.1573487099958584 | |
my implementation: | |
0.7448599760100478 | |
$ py3.3 time_them_2.py | |
default implementation: | |
0.6372028530022362 | |
my implementation: | |
0.20809232600731775 |
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
import timeit | |
print('default implementation:') | |
print(min(timeit.repeat(''' | |
Record = namedtuple('Record', 'name, value') | |
class Point(namedtuple('Point', 'x, y')): | |
'A fancy, fancy, 2d point' | |
@property | |
def hypot(self): | |
'p.hypot -> √(p.x² + p.y²)' | |
return (self.x ** 2 + self.y ** 2) ** 0.5 | |
def __str__(self): | |
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, | |
self.y, | |
self.hypot) | |
class EmployeeRecord(namedtuple('EmployeeRecord', 'name, age, title,' | |
'department, paygrade')): | |
def __init__(self, name, age, title, department, paygrade): | |
assert isinstance(paygrade, int) and paygrade >= 0, \ | |
'paygrade should be an integer >= 0, but is: {!r}'.format(paygrade) | |
''', | |
'from collections import namedtuple', number=1000, repeat=5))) | |
print('my implementation:') | |
print(min(timeit.repeat(''' | |
@namedtuple | |
def Record(name, value): | |
pass | |
@namedtuple | |
def Point(x, y): | |
'A fancy, fancy, 2d point' | |
@Point._property | |
def hypot(self): | |
'p.hypot -> √(p.x² + p.y²)' | |
return (self.x ** 2 + self.y ** 2) ** 0.5 | |
@Point._method | |
def __str__(self): | |
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, | |
self.y, | |
self.hypot) | |
@namedtuple | |
def EmployeeRecord(name, age, title, department, paygrade=1): | |
assert isinstance(paygrade, int) and paygrade >= 0, \ | |
'paygrade should be an integer >= 0, but is: {!r}'.format(paygrade) | |
''', | |
'from named import namedtuple', number=1000, repeat=5))) |
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
import timeit | |
print('default implementation:') | |
print(min(timeit.repeat(''' | |
Record = namedtuple('Record', 'name, value') | |
''', | |
'from collections import namedtuple', number=1000, repeat=10))) | |
print('my implementation:') | |
print(min(timeit.repeat(''' | |
@namedtuple | |
def Record(name, value): | |
pass | |
''', | |
'from named import namedtuple', number=1000, repeat=10))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment