convert tuple to string

Jack Diederich jack at performancedrivers.com
Mon Aug 11 23:16:41 CEST 2003


On Mon, Aug 11, 2003 at 08:40:13PM +0000, Alex Martelli wrote:
> 
> So, let's look at performance.  The complex, lambda-rich
> expression with a map and a reduce...:
> 
> python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])' 
> 'reduce(lambda x,y:x+","+y, map(lambda x:x[0],t))'
> 
> 10000 loops, best of 3: 59.3 usec per loop
> 
> The simple expression based on a list comprehension:
> 
> python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])' 
> '",".join([ x[0] for x in t ])'
> 
> 10000 loops, best of 3: 24.4 usec per loop
> 
> I think this is great fodder for the underground movement aiming
> to remove lambda, map and reduce.  All that complication just
> to slow things down by two times and a half...?-!

No, this is a call for lambda/map/filter fans to improve the
intepreter.  The above reduce has 2*30 more function calls
per loop than the equivalent list comprehension. The list comp
essentially gets inlined, the lambda gets called and the
arguments parsed each time.

A truer comparison would also have the top one doing ",".join or
the bottom one using reduce.  Having one doing 30*3 string concats
and the other doing one join() will skew the results.

-jackdied







More information about the Python-list mailing list