helper function in a class' namespace

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jan 31 17:39:02 EST 2008


On Thu, 31 Jan 2008 20:05:44 +0000, Stargaming wrote:

> String concatenation is generally considered unpythonic, better use
> string interpolation::
> 
>     'H> %s' % (M,)

String concatenation is significantly faster than string interpolation.

>>> import timeit
>>> timeit.Timer("'H> %s' % M", "M = 'abcdef'").repeat()
[1.3407769203186035, 0.69128704071044922, 0.56362509727478027]
>>> timeit.Timer("'H> ' + M", "M = 'abcdef'").repeat()
[0.69647812843322754, 0.69620108604431152, 0.65643787384033203]

The danger with string concatenation comes from building up a string 
piece by piece: even though a single + is faster than a single %, a dozen 
concats will likely be MUCH slower than a single &.

Also, the tuple above is totally unnecessary. 'H> %s' % M will work fine.


-- 
Steven



More information about the Python-list mailing list