classification
Title: Add variable access benchmark to Tools/Scripts
Type: performance Stage: resolved
Components: Demos and Tools Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, rhettinger
Priority: normal Keywords: patch, patch

Created on 2019-02-01 20:19 by rhettinger, last changed 2019-03-25 08:21 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11725 merged rhettinger, 2019-02-01 20:24
PR 11725 merged rhettinger, 2019-02-01 20:24
PR 11898 merged rhettinger, 2019-02-16 20:32
PR 11905 merged scoder, 2019-02-17 13:36
Messages (5)
msg334716 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-02-01 20:19
Adding a short script that I've found useful many times over the past decade. It has allowed my to quickly notice performance regressions and has helped identify places in need of optimization work.  It is also useful for building a empirical mental model of the relative cost of various kinds of variable access -- this is useful in writing high performance code.
msg334719 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-02-01 20:34
Here's what the output looks like (here am comparing a fresh Py3.8 with Clang versus the same build with GCC-8):

$ make distclean && ./configure && make
$ py Tools/scripts/var_access_benchmark.py
Speed of different kinds of variable accesses:
   4.2 μs	read_local
   4.7 μs	read_nonlocal
  13.7 μs	read_global
  18.5 μs	read_builtin
  18.2 μs	read_classvar
  26.8 μs	read_instancevar
  20.3 μs	read_instancevar_slots
  19.1 μs	read_namedtuple
  26.9 μs	read_boundmethod
   4.5 μs	write_local
   4.8 μs	write_nonlocal
  18.8 μs	write_global
  89.9 μs	write_classvar
  37.1 μs	write_instancevar
  25.7 μs	write_instancevar_slots
   0.3 μs	loop_overhead

$ make distclean && ./configure CC=gcc-8 && make
$ py Tools/scripts/var_access_benchmark.py
Speed of different kinds of variable accesses:
   4.0 μs	read_local
   4.2 μs	read_nonlocal
  11.4 μs	read_global
  16.3 μs	read_builtin
  16.4 μs	read_classvar
  24.8 μs	read_instancevar
  20.1 μs	read_instancevar_slots
  16.8 μs	read_namedtuple
  23.2 μs	read_boundmethod
   4.3 μs	write_local
   4.5 μs	write_nonlocal
  15.4 μs	write_global
  89.4 μs	write_classvar
  33.5 μs	write_instancevar
  24.5 μs	write_instancevar_slots
   0.3 μs	loop_overhead
msg334788 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-02-03 06:54
New changeset f75d59e1a896115bd52f543a417c665d6edc331f by Raymond Hettinger in branch 'master':
bpo-35884: Add variable access benchmarking script (GH-11725)
https://github.com/python/cpython/commit/f75d59e1a896115bd52f543a417c665d6edc331f
msg335718 - (view) Author: miss-islington (miss-islington) Date: 2019-02-16 20:51
New changeset 43766f82ddec84fad7a321eeec2e1cbff6ee44d2 by Miss Islington (bot) (Raymond Hettinger) in branch 'master':
 bpo-35884: Add missing FIFO queue benchmark (GH-11898)
https://github.com/python/cpython/commit/43766f82ddec84fad7a321eeec2e1cbff6ee44d2
msg338790 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-03-25 08:21
New changeset 68d228f174ceed151200e7e0b44ffc5edd4e0ea2 by Raymond Hettinger (Stefan Behnel) in branch 'master':
bpo-35884: Add string-keys-only microbenchmark for dict access to var_access_benchmark.py (GH-11905)
https://github.com/python/cpython/commit/68d228f174ceed151200e7e0b44ffc5edd4e0ea2
History
Date User Action Args
2019-03-25 08:21:01rhettingersetmessages: + msg338790
2019-02-17 13:36:32scodersetpull_requests: + pull_request11931
2019-02-16 20:51:05miss-islingtonsetnosy: + miss-islington
messages: + msg335718
2019-02-16 20:32:50rhettingersetpull_requests: + pull_request11925
2019-02-03 06:55:59rhettingersetkeywords: patch, patch
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-02-03 06:54:58rhettingersetmessages: + msg334788
2019-02-01 20:34:35rhettingersetkeywords: patch, patch

messages: + msg334719
2019-02-01 20:24:54rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request11607
2019-02-01 20:24:50rhettingersetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11606
2019-02-01 20:19:39rhettingercreate