Skip to content

Instantly share code, notes, and snippets.

@yipyip
Last active December 20, 2015 14:59
Show Gist options
  • Save yipyip/6150627 to your computer and use it in GitHub Desktop.
Save yipyip/6150627 to your computer and use it in GitHub Desktop.
string concatenation timings (Man beachte auch das letzte Ergebnis, da ist etwas Overhead beim Aufruf drin.)
In [41]: s = [str(i) for i in xrange(100)]
In [42]: timeit reduce(lambda x, y: x + y, s, '')
10000 loops, best of 3: 31.9 us per loop
In [43]: timeit "".join(s)
100000 loops, best of 3: 2.98 us per loop
In [44]: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p = s[:16]
In [45]: timeit a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p
1000000 loops, best of 3: 1.59 us per loop
In [46]: timeit "".join((a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p))
1000000 loops, best of 3: 1.24 us per loop
In [47]: timeit a+b+c
1000000 loops, best of 3: 300 ns per loop
In [48]: timeit ''.join((a, b, c))
1000000 loops, best of 3: 495 ns per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment