Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from ctypes import *
from timeit import Timer
from random import randint
from collections import namedtuple
class Point(object):
x, y = None, None
def __init__(self, x, y):
self.x, self.y = x, y
class Point_ctypes(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
class Point_slots(object):
__slots__=['x','y']
def __init__(self, x, y):
self.x, self.y = x, y
Point_namedtuple=namedtuple('Point_n',['x','y'])
def sum_(points):
sum_x,sum_y=0,0
for point in points:
sum_x+=point.x
sum_y+=point.y
return sum_x,sum_y
def rand_points(abs_point,n=10000):
return [abs_point(randint(0,1000),randint(0,1000)) for i in xrange(n)]
for i in (Point,Point_ctypes,Point_slots,Point_namedtuple):
points=rand_points(i)
print i.__name__,Timer('sum_(points)','from __main__ import sum_,points').timeit(number=1000)
# print i.__name__,Timer('sum([p.x for p in points]),sum([p.y for p in points])','from __main__ import sum_,points').timeit(number=1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.