Skip to content

Instantly share code, notes, and snippets.

@mawillcockson
Last active September 22, 2021 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mawillcockson/15a39ba9da0e0bdc20436069ae2bf24a to your computer and use it in GitHub Desktop.
Save mawillcockson/15a39ba9da0e0bdc20436069ae2bf24a to your computer and use it in GitHub Desktop.
Do short identifiers make python run faster?
#!/bin/sh
set -eu
A=0
IDENTIFIER="$(python3 -c 'print("a" * 100_000, end="")')"
export IDENTIFIER
echo "IDENTIFIER is ${#IDENTIFIER} characters"
for python_version in python3.6 python3.7 python3.8 python3.9; do
echo "${python_version}"
printf 'short identifier -> '
"${python_version}" -m timeit \
-s "a = 1" \
-s "${IDENTIFIER} = 1" \
-s "def short(): a + 1" \
-s "def long(): ${IDENTIFIER} + 1" \
"short()"
printf 'long identifier -> '
"${python_version}" -m timeit \
-s "a = 1" \
-s "${IDENTIFIER} = 1" \
-s "def short(): a + 1" \
-s "def long(): ${IDENTIFIER} + 1" \
"long()"
done
@mawillcockson
Copy link
Author

mawillcockson commented Sep 22, 2021

IDENTIFIER is 100000 characters
python3.6
short identifier -> 10000000 loops, best of 3: 0.113 usec per loop
long  identifier -> 10000000 loops, best of 3: 0.112 usec per loop
python3.7
short identifier -> 5000000 loops, best of 5: 84.6 nsec per loop
long  identifier -> 5000000 loops, best of 5: 81.2 nsec per loop
python3.8
short identifier -> 2000000 loops, best of 5: 104 nsec per loop
long  identifier -> 2000000 loops, best of 5: 104 nsec per loop
python3.9
short identifier -> 2000000 loops, best of 5: 110 nsec per loop
long  identifier -> 2000000 loops, best of 5: 114 nsec per loop

no appreciable difference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment