Ticket #4102: 4102-timings.py

File 4102-timings.py, 2.2 KB (added by Collin Grady <cgrady@…>, 17 years ago)

perf test script

Line 
1from timeit import Timer
2
3# A control model
4setup_a = '''
5class Model(object):
6 pass
7m = Model()
8'''
9
10# A model which has a __setattr__ which does nothing
11setup_b = '''
12class Model(object):
13 def __setattr__(self, name, value):
14 super(Model, self).__setattr__(name, value)
15m = Model()
16'''
17
18# A model which has a __setattr__ which matches this patch
19setup_c = '''
20class Model(object):
21 def __setattr__(self, name, value):
22 if name != '_modified_attrs' and (not hasattr(self, name) or
23 value != getattr(self, name)):
24 if hasattr(self, '_modified_attrs'):
25 if name not in self._modified_attrs:
26 self._modified_attrs.add(name)
27 else:
28 self._modified_attrs = set((name,))
29 super(Model, self).__setattr__(name, value)
30m = Model()
31'''
32
33# Optimized for speed, and dropped the value check as Brian suggested
34setup_d = '''
35class Model(object):
36 def __setattr__(self, name, value):
37 try:
38 if name not in self._modified_attrs:
39 self._modified_attrs.add(name)
40 except AttributeError:
41 if name != '_modified_attrs':
42 self._modified_attrs = set((name,))
43 super(Model, self).__setattr__(name, value)
44m = Model()
45'''
46
47setup_a1 = setup_a + '''
48m.another_attribute="test"
49'''
50
51setup_b1 = setup_b + '''
52m.another_attribute="test"
53'''
54
55setup_c1 = setup_c + '''
56m.another_attribute="test"
57'''
58
59setup_d1 = setup_d + '''
60m.another_attribute="test"
61'''
62
63print "Single Assignment"
64print "Control: ", Timer('m.an_attribute="test"', setup_a).timeit()
65print "noop: ", Timer('m.an_attribute="test"', setup_b).timeit()
66print "patch: ", Timer('m.an_attribute="test"', setup_c).timeit()
67print "optimized:", Timer('m.an_attribute="test"', setup_d).timeit()
68print
69print "Double Assignment"
70print "Control: ", Timer('m.an_attribute="test"', setup_a1).timeit()
71print "Noop: ", Timer('m.an_attribute="test"', setup_b1).timeit()
72print "Patch: ", Timer('m.an_attribute="test"', setup_c1).timeit()
73print "Optimized:", Timer('m.an_attribute="test"', setup_d1).timeit()
Back to Top