Help with cumulative sum

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Sep 10 11:52:53 CEST 2009


On Thu, 10 Sep 2009 09:55:29 +0200, Bruno Desthuilliers wrote:

> Maggie a écrit :
> 
> (snip - lots of answers and sensible suggestions already)
> 
>>    tmp_string = str(count) + '	' + item
> 
> Mays I suggest you learn about string formatting ?


Which is generally good advice, but for a once-off simple concatenation 
of three substrings, there's no great reason to prefer one over the 
other. There's no difference in length of code, little difference in 
readability, and concatenation is about 30% faster.


>>> from timeit import Timer
>>> Timer('str(count) + "    " + item', 
... 'count = 2345; item = "abcde"').repeat()
[0.98372197151184082, 0.90344786643981934, 0.9030919075012207]
>>> 
>>> Timer('"%d    %s" % (count, item)', 
... 'count = 2345; item = "abcde"').repeat()
[1.4281179904937744, 1.3027360439300537, 1.3032739162445068]



-- 
Steven



More information about the Python-list mailing list