Skip to content

Instantly share code, notes, and snippets.

@flyser
Forked from anonymous/gist:6183623
Last active December 20, 2015 19:29
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 flyser/6183740 to your computer and use it in GitHub Desktop.
Save flyser/6183740 to your computer and use it in GitHub Desktop.
## This is on python 2.7:
In [1]: a = '12345678'
In [2]: %timeit [a[max(i-3,0):i] for i in range(len(a), 0, -3)][::-1]
1000000 loops, best of 3: 1.91 us per loop
In [3]: %timeit format(int(a), ',').split(',')
1000000 loops, best of 3: 1.62 us per loop
In [4]: %timeit [a[max(0,i):i+3] for i in range((len(a)-1)%3-2, len(a), 3)]
1000000 loops, best of 3: 1.67 us per loop
## And python 3.2:
In [1]: a = '12345678'
In [2]: %timeit [a[max(i-3,0):i] for i in range(len(a), 0, -3)][::-1]
100000 loops, best of 3: 2.58 us per loop
In [3]: %timeit format(int(a), ',').split(',')
1000000 loops, best of 3: 1.98 us per loop
In [4]: %timeit [a[max(0,i):i+3] for i in range((len(a)-1)%3-2, len(a), 3)]
100000 loops, best of 3: 2.45 us per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment