Settings

Theme

Why Does Python Code Run Faster in a Function?

stackabuse.com

12 points by ScottWRobinson 2 years ago · 1 comment

Reader

raymondh 2 years ago

Note that this effect varies across builds and versions. For most of Python's history, we could give clear and invariant optimization advice (locals and nonlocals are fastest, global variable access was at least twice as slow, and builtin variable access was even slower). That ordering will likely remain true but absolute speeds have improved dramatically and the ratios have shifted).

Here is a run of Tools/scripts/var_access_benchmark.py for Python 3.12rc1 stock build for an Apple M1 Max (your mileage may vary):

    Variable and attribute read access:
       1.9 ns read_local
       2.4 ns read_nonlocal
       2.8 ns read_global
       4.1 ns read_builtin
       5.0 ns read_classvar_from_class
      12.1 ns read_classvar_from_instance
       4.8 ns read_instancevar
       4.7 ns read_instancevar_slots
      12.2 ns read_namedtuple
      29.0 ns read_boundmethod

    Variable and attribute write access:
       2.4 ns write_local
       2.5 ns write_nonlocal
      10.5 ns write_global
      26.8 ns write_classvar
       4.3 ns write_instancevar
       4.2 ns write_instancevar_slots

    Data structure read access:
       5.7 ns read_list
      11.1 ns read_deque
      10.1 ns read_dict
      10.5 ns read_strdict

    Data structure write access:
       6.2 ns write_list
      11.3 ns write_deque
      11.2 ns write_dict
      12.0 ns write_strdict

    Stack (or queue) operations:
      18.4 ns list_append_pop
      17.8 ns deque_append_pop
      18.1 ns deque_append_popleft

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection