""" Test strings concatenation >>> print 'one' + ' ' + 'two' >>> print "{} {}".format('one', 'two') >>> print "%s %s" % ('one', 'two') <== outdated >>> print ' '.join(['one', 'two']) """ import timeit import random import string def generate_random_strings(number=6, length=120): for index in range(number): #yield ''.join(random.sample(string.ascii_letters, length)) yield ''.join([random.choice(string.ascii_letters) for _ in range(length)]) RANDOM_STR_LIST = list(generate_random_strings()) def func1(str_list=RANDOM_STR_LIST): result = '' for s in str_list: result += s return result def func2(str_list=RANDOM_STR_LIST): result = '{}{}' for s in str_list: result.format(result, s) return result def func3(str_list=RANDOM_STR_LIST): result = ''.join(str_list) return result if __name__ == '__main__': print RANDOM_STR_LIST print "func1: {}".format( timeit.timeit(func1, number=100000) ) print "func2: {}".format( timeit.timeit(func3, number=100000) ) print "func3: {}".format( timeit.timeit(func3, number=100000) )