Skip to content

Instantly share code, notes, and snippets.

@idlesign
Created December 28, 2018 06:37
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 idlesign/817fb27ef2433df0d10c628bed630a51 to your computer and use it in GitHub Desktop.
Save idlesign/817fb27ef2433df0d10c628bed630a51 to your computer and use it in GitHub Desktop.
Python 3.7 string formatting variants speed measure
#!/bin/bash
measure () {
local RES=$(python3.7 -m timeit -n 2000000 -s 'x=256' "$1" | cut -d ":" -f 2)
echo "$1 -> $RES"
}
measure 'str(x)'
measure 'f"{x}"'
measure '"%d" % x'
measure '"{0}".format(x)'
@idlesign
Copy link
Author

str(x) ->  159 nsec per loop
f"{x}" ->  58.4 nsec per loop
"%d" % x ->  122 nsec per loop
"{0}".format(x) ->  218 nsec per loop

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