convert tuple to string

Anand Pillai pythonguy at Hotpop.com
Wed Aug 13 06:21:59 EDT 2003


Jack Diederich <jack at performancedrivers.com> wrote in message news:<mailman.1060636881.29626.python-list at python.org>...
> 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...?-!
> 

  I agree. Somehow the solutions which involve lambda +
functional tools (map, filter, reduce, apply) have an innate
appeal. Invariablye one finds that he has to use one
of (map, filter, apply, reduce) in his lambda expressions.
I did not check the performance part when I posted it.

Maybe the interpreter should decide to inline them lambda
calls also :-)

Anand

> No, t
his 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