Method much slower than function?

sjdevnull at yahoo.com sjdevnull at yahoo.com
Thu Jun 14 00:39:29 EDT 2007


Gabriel Genellina wrote:
> In the function above, s is a local variable, and accessing local
> variables is very efficient (using an array of local variables, the
> compiler assigns statically an index for each one).
> Using self.s, on the other hand, requires a name lookup for each access.
> The most obvious change would be to use a local variable s, and assign
> self.s = s only at the end. This should make both methods almost identical
> in performance.

Yeah, that's a big deal and makes a significant difference on my
machine.

> In addition, += is rather inefficient for strings; the usual idiom is
> using ''.join(items)

Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
strings pretty well.

a=""
for i in xrange(100000):
    a+="a"

and:

a=[]
for i in xrange(100000):
    a.append("a")
a="".join(a)

take virtually the same amount of time on my machine (2.5), and the
non-join version is clearer, IMO.  I'd still use join in case I wind
up running under an older Python, but it's probably not a big issue
here.

> And since you have Python 2.5, you can use the file as its own iterator;
> combining all this:
>
>      return ''.join(line.strip() for line in filehandle if '>' not in line)

That's probably pretty good.




More information about the Python-list mailing list