Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>&gt;&gt;&gt; import timeit &gt;&gt;&gt; def so_q_sub(n): ... return "%s%s/%d" % (DOMAIN, QUESTIONS, n) ... &gt;&gt;&gt; so_q_sub(1000) 'http://stackoverflow.com/questions/1000' &gt;&gt;&gt; def so_q_cat(n): ... return DOMAIN + QUESTIONS + '/' + str(n) ... &gt;&gt;&gt; so_q_cat(1000) 'http://stackoverflow.com/questions/1000' &gt;&gt;&gt; t1 = timeit.Timer('so_q_sub(1000)','from __main__ import so_q_sub') &gt;&gt;&gt; t2 = timeit.Timer('so_q_cat(1000)','from __main__ import so_q_cat') &gt;&gt;&gt; t1.timeit(number=10000000) 12.166618871951641 &gt;&gt;&gt; t2.timeit(number=10000000) 5.7813972166853773 &gt;&gt;&gt; t1.timeit(number=1) 1.103492206766532e-05 &gt;&gt;&gt; t2.timeit(number=1) 8.5206360154188587e-06 &gt;&gt;&gt; def so_q_tmp(n): ... return "{d}{q}/{n}".format(d=DOMAIN,q=QUESTIONS,n=n) ... &gt;&gt;&gt; so_q_tmp(1000) 'http://stackoverflow.com/questions/1000' &gt;&gt;&gt; t3= timeit.Timer('so_q_tmp(1000)','from __main__ import so_q_tmp') &gt;&gt;&gt; t3.timeit(number=10000000) 14.564135316080637 &gt;&gt;&gt; def so_q_join(n): ... return ''.join([DOMAIN,QUESTIONS,'/',str(n)]) ... &gt;&gt;&gt; so_q_join(1000) 'http://stackoverflow.com/questions/1000' &gt;&gt;&gt; t4= timeit.Timer('so_q_join(1000)','from __main__ import so_q_join') &gt;&gt;&gt; t4.timeit(number=10000000) 9.4431309007150048 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload