Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # timeit_string_concatenation.py
- import timeit
- s = 'hello'
- t = 'world'
- str_dict = {'t': t, 's': s}
- test = '''
- s + ' ' + t
- ' '.join((s, t))
- '%s %s' % (s, t)
- '{} {}'.format(s, t)
- '{0} {1}'.format(s, t)
- '{str1} {str2}'.format(str1=s, str2=t)
- '{s} {t}'.format(**globals())
- '%(s)s %(t)s' % str_dict
- '{s} {t}'.format(**str_dict)
- '''[1:-1].splitlines()
- def fn(x=0):
- if x:
- return test[z]
- return eval(test[z])
- iterations = 10000
- for z in xrange(len(test)):
- repeat = []
- print fn(1)+' = "'+fn()+'"'
- for n in xrange(10):
- x = 'from __main__ import fn; fn()'
- ttt = timeit.Timer(x)
- x = ttt.timeit(iterations)
- repeat += [x]
- x = min(repeat)
- print 'took {} seconds : best out of {}'.format(x,len(repeat))
- print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement