Skip to content

Instantly share code, notes, and snippets.

@badbye
Created December 5, 2017 07:43
Show Gist options
  • Save badbye/fd8aaa35cda4fbc4c41a3a4f78dcab4f to your computer and use it in GitHub Desktop.
Save badbye/fd8aaa35cda4fbc4c41a3a4f78dcab4f to your computer and use it in GitHub Desktop.
Compare `%s` with the `format` method in python
import time
# python3.6.1: `%s` is 10 tiems faster than `format`
>>> t1=time.time();c=['%s %s' %(1, 2) for i in range(10000)];time.time()-t1
0.0005083084106445312
>>> t1=time.time();c=['{0} {1}'.format(1, 2) for i in range(10000)];time.time()-t1
0.005129098892211914
# python2.7.10 `%s` is 2 tiems faster than `format`
>>> t1=time.time();c=['{0} {1}'.format(1, 2) for i in range(10000)];time.time()-t1
0.00415802001953125
>>> t1=time.time();c=['%s %s' %(1, 2) for i in range(10000)];time.time()-t1
0.002238035202026367
# Python3 VS Python2: `%s` is faster, and `format` is slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment