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
|
|
|
|
|
|
|
|
v_global = 1
|
|
|
|
|
|
|
|
def read_local():
|
|
|
|
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():
|
|
|
|
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
|
|
|
|
|
|
|
|
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():
|
|
|
|
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
|
|
|
_a = _A()
|
|
|
|
_a.x = 1
|
|
|
|
|
2021-04-14 16:25:22 +03:00
|
|
|
def read_instancevar():
|
2021-04-17 13:32:02 +03:00
|
|
|
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():
|
|
|
|
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
|
|
|
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():
|
|
|
|
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():
|
|
|
|
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():
|
|
|
|
v_nonlocal = 1
|
|
|
|
def inner():
|
|
|
|
nonlocal v_nonlocal
|
|
|
|
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
|
|
|
|
|
|
|
|
write_nonlocal = make_nonlocal_writer()
|
|
|
|
|
|
|
|
def write_global():
|
|
|
|
global v_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():
|
|
|
|
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
|
|
|
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__':
|
2021-04-16 13:58:05 +03:00
|
|
|
from fasttimer import timeit
|
2021-04-14 16:25:22 +03:00
|
|
|
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]:
|
2022-06-02 02:01:55 +03:00
|
|
|
print(min(timeit(f,number=100000) for x in range(10)), f.__qualname__ if hasattr(f,'__qualname__') else f.__name__ if hasattr(f,'__name__') else '?')
|
2021-04-14 16:25:22 +03:00
|
|
|
|