Method much slower than function?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Jun 13 21:29:55 EDT 2007
En Wed, 13 Jun 2007 21:40:12 -0300, <idoerg at gmail.com> escribió:
> Hi all,
>
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.
>
> ################# START SOURCE #############
> # The function
>
> def readgenome(filehandle):
> s = ''
> for line in filehandle.xreadlines():
> if '>' in line:
> continue
> s += line.strip()
> return s
>
> # The method in a class
> class bar:
> def readgenome(self, filehandle):
> self.s = ''
> for line in filehandle.xreadlines():
> if '>' in line:
> continue
> self.s += line.strip()
>
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.
In addition, += is rather inefficient for strings; the usual idiom is
using ''.join(items)
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)
--
Gabriel Genellina
More information about the Python-list
mailing list