String concatenation vs. string substitution in Python String concatenation vs. string substitution in Python python python

String concatenation vs. string substitution in Python


Concatenation is (significantly) faster according to my machine. But stylistically, I'm willing to pay the price of substitution if performance is not critical. Well, and if I need formatting, there's no need to even ask the question... there's no option but to use interpolation/templating.

>>> import timeit>>> def so_q_sub(n):...  return "%s%s/%d" % (DOMAIN, QUESTIONS, n)...>>> so_q_sub(1000)'http://stackoverflow.com/questions/1000'>>> def so_q_cat(n):...  return DOMAIN + QUESTIONS + '/' + str(n)...>>> so_q_cat(1000)'http://stackoverflow.com/questions/1000'>>> t1 = timeit.Timer('so_q_sub(1000)','from __main__ import so_q_sub')>>> t2 = timeit.Timer('so_q_cat(1000)','from __main__ import so_q_cat')>>> t1.timeit(number=10000000)12.166618871951641>>> t2.timeit(number=10000000)5.7813972166853773>>> t1.timeit(number=1)1.103492206766532e-05>>> t2.timeit(number=1)8.5206360154188587e-06>>> def so_q_tmp(n):...  return "{d}{q}/{n}".format(d=DOMAIN,q=QUESTIONS,n=n)...>>> so_q_tmp(1000)'http://stackoverflow.com/questions/1000'>>> t3= timeit.Timer('so_q_tmp(1000)','from __main__ import so_q_tmp')>>> t3.timeit(number=10000000)14.564135316080637>>> def so_q_join(n):...  return ''.join([DOMAIN,QUESTIONS,'/',str(n)])...>>> so_q_join(1000)'http://stackoverflow.com/questions/1000'>>> t4= timeit.Timer('so_q_join(1000)','from __main__ import so_q_join')>>> t4.timeit(number=10000000)9.4431309007150048


Don't forget about named substitution:

def so_question_uri_namedsub(q_num):    return "%(domain)s%(questions)s/%(q_num)d" % locals()


Be wary of concatenating strings in a loop! The cost of string concatenation is proportional to the length of the result. Looping leads you straight to the land of N-squared. Some languages will optimize concatenation to the most recently allocated string, but it's risky to count on the compiler to optimize your quadratic algorithm down to linear. Best to use the primitive (join?) that takes an entire list of strings, does a single allocation, and concatenates them all in one go.