Hacker News new | ask | show | jobs
by L3viathan 854 days ago
Probably because the string is so short.
1 comments

Yeah, it's not great with long strings. f-strings seem to be great just about always though. Highly recommend.

  $ python3.12 -m timeit -s 'import string' -s 'x = string.printable * 50' 'f"Welcome to {x}."'
  2000000 loops, best of 5: 106 nsec per loop
  $ python3.12 -m timeit -s 'import string' -s 'x = string.printable * 50' '"Welcome to {}.".format(x)'
  2000000 loops, best of 5: 200 nsec per loop
  $ python3.12 -m timeit -s 'import string' -s 'x = string.printable * 50' '"Welcome to %s." % x'
  2000000 loops, best of 5: 169 nsec per loop
  $ python3.12 -m timeit -s 'import string' -s 'x = string.printable * 50' '"Welcome to " + str(x) + "."'
  1000000 loops, best of 5: 214 nsec per loop
  $ python3.12 -m timeit -s 'import string' -s 'x = string.printable * 50' '"Welcome to " + x + "."'
  1000000 loops, best of 5: 198 nsec per loop
len(string.printable * 50) = 5000