Welcome, guest | Sign In | My Account | Store | Cart
# Show relative speeds of local, nonlocal, global, and built-in access.

trials = [None] * 500

def read_local(trials=trials):
    v_local = None
    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 = None
    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 = None
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

class A:
    pass

def read_classvar(trials=trials, A=A):
    A.x = None
    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=A()):
    a.x = None
    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 write_local(trials=trials):
    v_local = None
    for t in trials:
        v_local = None; v_local = None; v_local = None; v_local = None; v_local = None
        v_local = None; v_local = None; v_local = None; v_local = None; v_local = None
        v_local = None; v_local = None; v_local = None; v_local = None; v_local = None
        v_local = None; v_local = None; v_local = None; v_local = None; v_local = None
        v_local = None; v_local = None; v_local = None; v_local = None; v_local = None

def make_nonlocal_writer():
    v_nonlocal = None
    def inner(trials=trials):
        nonlocal v_nonlocal
        for t in trials:
            v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None
            v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None
            v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None
            v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None
            v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None; v_nonlocal = None
    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 = None; v_global = None; v_global = None; v_global = None; v_global = None
        v_global = None; v_global = None; v_global = None; v_global = None; v_global = None
        v_global = None; v_global = None; v_global = None; v_global = None; v_global = None
        v_global = None; v_global = None; v_global = None; v_global = None; v_global = None
        v_global = None; v_global = None; v_global = None; v_global = None; v_global = None

def write_classvar(trials=trials, A=A):
    for t in trials:
        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None

def write_instancevar(trials=trials, a=A()):
    for t in trials:
        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None

def loop_overhead(trials=trials):
    for t in trials:
        pass


if __name__=='__main__':
    from timeit import Timer

    for f in [read_local, read_nonlocal, read_global, read_builtin,
              read_classvar, read_instancevar,
              write_local, write_nonlocal, write_global,
              write_classvar, write_instancevar,
              loop_overhead]:
        print('{:5.3f}\t{}'.format(min(Timer(f).repeat(7, 1000)), f.__name__))

Diff to Previous Revision

--- revision 1 2011-08-09 21:27:27
+++ revision 2 2011-08-10 03:51:39
@@ -35,13 +35,33 @@
         v_global; v_global; v_global; v_global; v_global
 
 def read_builtin(trials=trials):
-    v_local = None
     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
+
+class A:
+    pass
+
+def read_classvar(trials=trials, A=A):
+    A.x = None
+    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=A()):
+    a.x = None
+    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 write_local(trials=trials):
     v_local = None
@@ -76,6 +96,22 @@
         v_global = None; v_global = None; v_global = None; v_global = None; v_global = None
         v_global = None; v_global = None; v_global = None; v_global = None; v_global = None
 
+def write_classvar(trials=trials, A=A):
+    for t in trials:
+        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
+        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
+        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
+        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
+        A.x = None;    A.x = None;    A.x = None;    A.x = None;    A.x = None
+
+def write_instancevar(trials=trials, a=A()):
+    for t in trials:
+        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
+        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
+        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
+        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
+        a.x = None;    a.x = None;    a.x = None;    a.x = None;    a.x = None
+
 def loop_overhead(trials=trials):
     for t in trials:
         pass
@@ -85,5 +121,8 @@
     from timeit import Timer
 
     for f in [read_local, read_nonlocal, read_global, read_builtin,
-              write_local, write_nonlocal, write_global, loop_overhead]:
-        print(min(Timer(f).repeat(7, 1000)), f.__name__, sep='\t')
+              read_classvar, read_instancevar,
+              write_local, write_nonlocal, write_global,
+              write_classvar, write_instancevar,
+              loop_overhead]:
+        print('{:5.3f}\t{}'.format(min(Timer(f).repeat(7, 1000)), f.__name__))

History