File information | |||
---|---|---|---|
Filename: | object_pools.py | Uploaded: | Sat, 18th Sep 2010 17:14:50 |
Size (bytes): | 4.1 KiB | md5 checksum: | 4424246c085b14772ea135d3b597d6fd |
Uploader | doug | Download: | object_pools.py |
Description: |
Profiling object pools in Python |
import timeit number = 1000000 class OneProperty(object): def __init__(self, a): self.a = a class ManyProperty(object): def __init__(self, a, b, c, d, e, f, g, h, i, j): self.a = a self.b = b self.c = c self.d = d self.e = e self.f = f self.g = g self.h = h self.i = i self.j = j class ExpensiveInit(ManyProperty): """This one has an expensive setup.""" def __init__(self): self.data = range(300) class ObjectPool(object): init_size = number / 2 expand_by = int(init_size * 0.1) pool_class = object def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs self.pool = [self.pool_class(*self.args, **self.kwargs) for i in xrange(self.init_size)] def checkout(self): """Take out an item""" try: item = self.pool.pop() except IndexError: self.expand(self.expand_by) item = self.pool.pop() return item def checkin(self, obj): """Return an item to the pool""" return self.pool.append(obj) def expand(self, n): """Expand the pool by n items.""" for i in xrange(n): self.pool.append( self.pool_class(*self.args, **self.kwargs) ) class OnePropertyPool(ObjectPool): pool_class = OneProperty class ManyPropertyPool(ObjectPool): pool_class = ManyProperty class ExpensiveInitPool(ObjectPool): pool_class = ExpensiveInit # --------------------------------------------------------------------- t1 = timeit.timeit( stmt = "object()", number = number) print "Init object() %d times = %.2f usec/pass" % (number, number * t1/number) t2 = timeit.timeit( stmt = "pool.checkin(pool.checkout())", setup = "from %s import ObjectPool\npool = ObjectPool()" % __name__, number = number) print "Using pools for object() %d times = %.2f usec/pass" % (number, number * t2/number) # --------------------------------------------------------------------- t3 = timeit.timeit( stmt = "OneProperty(0)", setup = "from %s import OneProperty" % __name__, number = number) print "Init OneProperty(0) %d times = %.2f usec/pass" % (number, number * t3/number) t4 = timeit.timeit( stmt = "pool.checkin(pool.checkout())", setup = "from %s import OnePropertyPool\npool = OnePropertyPool(0)" % __name__, number = number) print "Using pools for OneProperty(0) %d times = %.2f usec/pass" % (number, number * t4/number) # --------------------------------------------------------------------- t5 = timeit.timeit( stmt = "ManyProperty(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)", setup = "from %s import ManyProperty" % __name__, number = number) print "Init ManyProperty(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) %d times = %.2f usec/pass" % (number, number * t5/number) t6 = timeit.timeit( stmt = "pool.checkin(pool.checkout())", setup = "from %s import ManyPropertyPool\npool = ManyPropertyPool(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)" % __name__, number = number) print "Using pools for ManyPropertyPool(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) %d times = %.2f usec/pass" % (number, number * t6/number) # --------------------------------------------------------------------- t7 = timeit.timeit( stmt = "ExpensiveInit()", setup = "from %s import ExpensiveInit" % __name__, number = number) print "Init ExpensiveInit() %d times = %.2f usec/pass" % (number, number * t7/number) t8 = timeit.timeit( stmt = "pool.checkin(pool.checkout())", setup = "from %s import ExpensiveInitPool\npool = ExpensiveInitPool()" % __name__, number = number) print "Using pools for ExpensiveInitPool() %d times = %.2f usec/pass" % (number, number * t8/number) # ---------------------------------------------------------------------