-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_flyweight.py
More file actions
211 lines (149 loc) · 4.5 KB
/
Copy pathtest_flyweight.py
File metadata and controls
211 lines (149 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
""" Speed performance tests.
Copyright (c) 2014, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
License: MIT
"""
loops = range(1000)
### Fly objects
class Fly:
def __init__(self, a, b):
self.a = a
self.b = b
class FlyObject(object):
def __init__(self, a, b):
self.a = a
self.b = b
class FlySlots(object):
__slots__ = ('a', 'b')
def __init__(self, a, b):
self.a = a
self.b = b
### Fly object storages
class FlyStorage(list):
counter = -1
Fly = Fly
max_storage = len(loops)
def __init__(self, max_storage=None):
if max_storage is None:
max_storage = self.max_storage
list.__init__(self, (None,) * max_storage)
def add_fly(self, *args):
f = self.Fly(*args)
self.counter += 1
i = self.counter
# generic:
#self.data[i] = tuple(f.__dict__.items())
self[i] = (f.a, f.b)
return i
def get_fly(self, i):
f = self.Fly(0, 0)
# generic:
#f.__dict__ = dict(self.data[i])
f.a, f.b = self[i]
return f
class FlyObjectStorage(FlyStorage):
Fly = FlyObject
class FlySlotsStorage(FlyStorage):
Fly = FlySlots
def add_fly(self, *args):
f = self.Fly(*args)
self.counter += 1
i = self.counter
# generic:
#self[i] = tuple(getattr(f, x) for x in f.__slots__)
self[i] = (f.a, f.b)
return i
def get_fly(self, i):
f = self.Fly(0, 0)
f.a, f.b = self[i]
return f
### Minimized Fly objects
class MiniFly(tuple):
def __new__(cls, *args):
f = Fly(*args)
return tuple.__new__(cls, (f.a, f.b))
def emerge(self):
return Fly(*self)
class MiniFlyObject(tuple):
def __new__(cls, *args):
f = FlyObject(*args)
return tuple.__new__(cls, (f.a, f.b))
def emerge(self):
return FlyObject(*self)
class MiniFlySlots(tuple):
def __new__(cls, *args):
f = FlySlots(*args)
return tuple.__new__(cls, (f.a, f.b))
def emerge(self):
return FlySlots(*self)
### Fly objects
def create_list_of_fly_objects():
l = [Fly(3, 4) for i in loops]
return l
assert len(create_list_of_fly_objects()) == len(loops)
def create_list_of_fly_flyweights():
storage = FlyStorage()
for i in loops:
storage.add_fly(3, 4)
return storage
storage = create_list_of_fly_flyweights()
assert len(storage) == len(loops)
assert isinstance(storage.get_fly(1), Fly)
filled_fly_storage = storage
def retrieve_list_of_fly_flyweights():
storage = filled_fly_storage
l = [storage.get_fly(i) for i in loops]
return l
def create_list_of_miniflys():
l = [MiniFly(3, 4) for i in loops]
return l
### FlyObject objects
def create_list_of_flyobject_objects():
l = [FlyObject(3, 4) for i in loops]
return l
assert len(create_list_of_flyobject_objects()) == len(loops)
def create_list_of_flyobject_flyweights():
storage = FlyObjectStorage()
for i in loops:
storage.add_fly(3, 4)
return storage
storage = create_list_of_flyobject_flyweights()
assert len(storage) == len(loops)
assert isinstance(storage.get_fly(1), FlyObject)
filled_flyobject_storage = storage
def retrieve_list_of_flyobject_flyweights():
storage = filled_flyobject_storage
l = [storage.get_fly(i) for i in loops]
return l
def create_list_of_miniflyobjects():
l = [MiniFlyObject(3, 4) for i in loops]
return l
assert len(create_list_of_miniflyobjects()) == len(loops)
### FlySlots objects
def create_list_of_flyslots_objects():
l = [FlySlots(3, 4) for i in loops]
return l
assert len(create_list_of_flyslots_objects()) == len(loops)
def create_list_of_flyslots_flyweights():
storage = FlySlotsStorage()
for i in loops:
storage.add_fly(3, 4)
return storage
storage = create_list_of_flyslots_flyweights()
assert len(storage) == len(loops)
assert isinstance(storage.get_fly(1), FlySlots)
filled_flyslots_storage = storage
def retrieve_list_of_flyslots_flyweights():
storage = filled_flyslots_storage
l = [storage.get_fly(i) for i in loops]
return l
def create_list_of_miniflyslots():
l = [MiniFlySlots(3, 4) for i in loops]
return l
###
if __name__ == '__main__':
import perftools
perftools.time_functions(globals())
print
perftools.memory_check_functions(globals())