2021-04-14 16:25:22 +03:00
|
|
|
# Show relative speeds of local, nonlocal, global, and built-in access.
|
|
|
|
class _A(object):
|
|
|
|
def m(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
let v_global = 1
|
|
|
|
|
|
|
|
def read_local():
|
|
|
|
let v_local = 1
|
|
|
|
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():
|
|
|
|
let v_nonlocal = 1
|
|
|
|
def inner():
|
|
|
|
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
|
|
|
|
return inner
|
|
|
|
|
|
|
|
let read_nonlocal = make_nonlocal_reader()
|
|
|
|
|
|
|
|
def read_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; v_global
|
|
|
|
|
|
|
|
def read_builtin():
|
|
|
|
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
|
|
|
|
|
2021-04-17 13:32:02 +03:00
|
|
|
_A.x = 1
|
|
|
|
|
2021-04-14 16:25:22 +03:00
|
|
|
def read_classvar():
|
|
|
|
let A = _A
|
|
|
|
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
|
|
|
|
|
2021-04-17 13:32:02 +03:00
|
|
|
let _a = _A()
|
|
|
|
_a.x = 1
|
2021-04-14 16:25:22 +03:00
|
|
|
def read_instancevar():
|
2021-04-17 13:32:02 +03:00
|
|
|
let a = _a
|
2021-04-14 16:25:22 +03:00
|
|
|
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_unboundmethod():
|
|
|
|
let A = _A
|
|
|
|
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 read_boundmethod():
|
2021-04-17 13:32:02 +03:00
|
|
|
let a = _a
|
2021-04-14 16:25:22 +03:00
|
|
|
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
|
|
|
|
|
2022-05-05 02:23:49 +03:00
|
|
|
def call_boundmethod():
|
|
|
|
let a = _a
|
|
|
|
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()
|
|
|
|
|
2021-04-14 16:25:22 +03:00
|
|
|
def write_local():
|
|
|
|
let 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; v_local = 1
|
|
|
|
|
|
|
|
def make_nonlocal_writer():
|
|
|
|
let v_nonlocal = 1
|
|
|
|
def inner():
|
|
|
|
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
|
|
|
|
return inner
|
|
|
|
|
|
|
|
let write_nonlocal = make_nonlocal_writer()
|
|
|
|
|
|
|
|
def write_global():
|
|
|
|
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():
|
|
|
|
let A = _A
|
|
|
|
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():
|
2021-04-17 13:32:02 +03:00
|
|
|
let a = _a
|
2021-04-14 16:25:22 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
from timeit import timeit
|
|
|
|
for f in [read_local, read_nonlocal, read_global, read_builtin,
|
2022-05-05 02:23:49 +03:00
|
|
|
read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod,
|
2021-04-14 16:25:22 +03:00
|
|
|
write_local, write_nonlocal, write_global,
|
|
|
|
write_classvar, write_instancevar]:
|
|
|
|
print(timeit(f,number=1000000), f.__qualname__)
|
|
|
|
|