Method much slower than function?

Steven D'Aprano steve at
Thu Jun 14 19:17:25 CEST 2007

On Thu, 14 Jun 2007 00:40:12 +0000, idoerg wrote:

>          20004 function calls in 10.214 CPU seconds

This calls the method on the CLASS, instead of an instance. When I try it,
I get this:

TypeError: unbound method readgenome() must be called with bar instance as
first argument (got file instance instead)

So you're running something subtly different than what you think you're
running. Maybe you assigned bar = bar() at some point?

However, having said that, the speed difference does seem to be real: even
when I correct the above issue, I get a large time difference using
either or, and timeit agrees:

>>> f = bar().readgenome
>>> timeit.Timer("f(open('cb_foo'))", "from __main__ import f").timeit(5)
>>> timeit.Timer("readgenome(open('cb_foo'))", "from __main__ import readgenome").timeit(5)

That's a difference of two orders of magnitude, and I can't see why.


More information about the Python-list mailing list