Method much slower than function?

Christof Winter winter at biotec.tu-dresden.de
Thu Jun 14 06:36:08 EDT 2007


Gabriel Genellina wrote:
[...]
> py> import timeit
> py>
> py> def f1():
> ...   a=""
> ...   for i in xrange(100000):
> ...       a+=str(i)*20
> ...
> py> def f2():
> ...   a=[]
> ...   for i in xrange(100000):
> ...       a.append(str(i)*20)
> ...   a="".join(a)
> ...
> py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
> 
> ...after a few minutes I aborted the process...
> 

Using

Python 2.4.4 (#2, Jan 13 2007, 17:50:26)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2

f1() and f2() also virtually take the same amount of time, although I must admit 
that this is quite different from what I expected.

Cheers,
Christof



More information about the Python-list mailing list