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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
'Show relative speeds of local, nonlocal, global, and built-in access.'
# Please leave this code so that it runs under older versions of
# Python 3 (no f-strings). That will allow benchmarking for
# cross-version comparisons. To run the benchmark on Python 2,
# comment-out the nonlocal reads and writes.
from collections import deque, namedtuple
trials = [None] * 500
steps_per_trial = 25
class A(object):
def m(self):
pass
class B(object):
__slots__ = 'x'
def __init__(self, x):
self.x = x
class C(object):
def __init__(self, x):
self.x = x
def read_local(trials=trials):
v_local = 1
for t in trials:
v_local; v_local; v_local; v_local; v_local
v_local; v_local; v_local; v_local; v_local
v_local; v_local; v_local; v_local; v_local
v_local; v_local; v_local; v_local; v_local
v_local; v_local; v_local; v_local; v_local
def make_nonlocal_reader():
v_nonlocal = 1
def inner(trials=trials):
for t in trials:
v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal
v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal
v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal
v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal
v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal; v_nonlocal
inner.__name__ = 'read_nonlocal'
return inner
read_nonlocal = make_nonlocal_reader()
v_global = 1
def read_global(trials=trials):
for t in trials:
v_global; v_global; v_global; v_global; v_global
v_global; v_global; v_global; v_global; v_global
v_global; v_global; v_global; v_global; v_global
v_global; v_global; v_global; v_global; v_global
v_global; v_global; v_global; v_global; v_global
def read_builtin(trials=trials):
for t in trials:
oct; oct; oct; oct; oct
oct; oct; oct; oct; oct
oct; oct; oct; oct; oct
oct; oct; oct; oct; oct
oct; oct; oct; oct; oct
def read_classvar_from_class(trials=trials, A=A):
A.x = 1
for t in trials:
A.x; A.x; A.x; A.x; A.x
A.x; A.x; A.x; A.x; A.x
A.x; A.x; A.x; A.x; A.x
A.x; A.x; A.x; A.x; A.x
A.x; A.x; A.x; A.x; A.x
def read_classvar_from_instance(trials=trials, A=A):
A.x = 1
a = A()
for t in trials:
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
def read_instancevar(trials=trials, a=C(1)):
for t in trials:
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
def read_instancevar_slots(trials=trials, a=B(1)):
for t in trials:
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
def read_namedtuple(trials=trials, D=namedtuple('D', ['x'])):
a = D(1)
for t in trials:
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
a.x; a.x; a.x; a.x; a.x
def read_boundmethod(trials=trials, a=A()):
for t in trials:
a.m; a.m; a.m; a.m; a.m
a.m; a.m; a.m; a.m; a.m
a.m; a.m; a.m; a.m; a.m
a.m; a.m; a.m; a.m; a.m
a.m; a.m; a.m; a.m; a.m
def write_local(trials=trials):
v_local = 1
for t in trials:
v_local = 1; v_local = 1; v_local = 1; v_local = 1; v_local = 1
v_local = 1; v_local = 1; v_local = 1; v_local = 1; v_local = 1
v_local = 1; v_local = 1; v_local = 1; v_local = 1; v_local = 1
v_local = 1; v_local = 1; v_local = 1; v_local = 1; v_local = 1
v_local = 1; v_local = 1; v_local = 1; v_local = 1; v_local = 1
def make_nonlocal_writer():
v_nonlocal = 1
def inner(trials=trials):
nonlocal v_nonlocal
for t in trials:
v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1
v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1
v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1
v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1
v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1; v_nonlocal = 1
inner.__name__ = 'write_nonlocal'
return inner
write_nonlocal = make_nonlocal_writer()
def write_global(trials=trials):
global v_global
for t in trials:
v_global = 1; v_global = 1; v_global = 1; v_global = 1; v_global = 1
v_global = 1; v_global = 1; v_global = 1; v_global = 1; v_global = 1
v_global = 1; v_global = 1; v_global = 1; v_global = 1; v_global = 1
v_global = 1; v_global = 1; v_global = 1; v_global = 1; v_global = 1
v_global = 1; v_global = 1; v_global = 1; v_global = 1; v_global = 1
def write_classvar(trials=trials, A=A):
for t in trials:
A.x = 1; A.x = 1; A.x = 1; A.x = 1; A.x = 1
A.x = 1; A.x = 1; A.x = 1; A.x = 1; A.x = 1
A.x = 1; A.x = 1; A.x = 1; A.x = 1; A.x = 1
A.x = 1; A.x = 1; A.x = 1; A.x = 1; A.x = 1
A.x = 1; A.x = 1; A.x = 1; A.x = 1; A.x = 1
def write_instancevar(trials=trials, a=C(1)):
for t in trials:
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
def write_instancevar_slots(trials=trials, a=B(1)):
for t in trials:
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
a.x = 1; a.x = 1; a.x = 1; a.x = 1; a.x = 1
def read_list(trials=trials, a=[1]):
for t in trials:
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
def read_deque(trials=trials, a=deque([1])):
for t in trials:
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
def read_dict(trials=trials, a={0: 1}):
for t in trials:
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
a[0]; a[0]; a[0]; a[0]; a[0]
def read_strdict(trials=trials, a={'key': 1}):
for t in trials:
a['key']; a['key']; a['key']; a['key']; a['key']
a['key']; a['key']; a['key']; a['key']; a['key']
a['key']; a['key']; a['key']; a['key']; a['key']
a['key']; a['key']; a['key']; a['key']; a['key']
a['key']; a['key']; a['key']; a['key']; a['key']
def list_append_pop(trials=trials, a=[1]):
ap, pop = a.append, a.pop
for t in trials:
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
def deque_append_pop(trials=trials, a=deque([1])):
ap, pop = a.append, a.pop
for t in trials:
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop()
def deque_append_popleft(trials=trials, a=deque([1])):
ap, pop = a.append, a.popleft
for t in trials:
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop();
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop();
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop();
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop();
ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop(); ap(1); pop();
def write_list(trials=trials, a=[1]):
for t in trials:
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
def write_deque(trials=trials, a=deque([1])):
for t in trials:
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
def write_dict(trials=trials, a={0: 1}):
for t in trials:
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
a[0]=1; a[0]=1; a[0]=1; a[0]=1; a[0]=1
def write_strdict(trials=trials, a={'key': 1}):
for t in trials:
a['key']=1; a['key']=1; a['key']=1; a['key']=1; a['key']=1
a['key']=1; a['key']=1; a['key']=1; a['key']=1; a['key']=1
a['key']=1; a['key']=1; a['key']=1; a['key']=1; a['key']=1
a['key']=1; a['key']=1; a['key']=1; a['key']=1; a['key']=1
a['key']=1; a['key']=1; a['key']=1; a['key']=1; a['key']=1
def loop_overhead(trials=trials):
for t in trials:
pass
if __name__=='__main__':
from timeit import Timer
for f in [
'Variable and attribute read access:',
read_local, read_nonlocal, read_global, read_builtin,
read_classvar_from_class, read_classvar_from_instance,
read_instancevar, read_instancevar_slots,
read_namedtuple, read_boundmethod,
'\nVariable and attribute write access:',
write_local, write_nonlocal, write_global,
write_classvar, write_instancevar, write_instancevar_slots,
'\nData structure read access:',
read_list, read_deque, read_dict, read_strdict,
'\nData structure write access:',
write_list, write_deque, write_dict, write_strdict,
'\nStack (or queue) operations:',
list_append_pop, deque_append_pop, deque_append_popleft,
'\nTiming loop overhead:',
loop_overhead]:
if isinstance(f, str):
print(f)
continue
timing = min(Timer(f).repeat(7, 1000))
timing *= 1000000 / (len(trials) * steps_per_trial)
print('{:6.1f} ns\t{}'.format(timing, f.__name__))
|