Method much slower than function?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Jun 14 19:17:25 CEST 2007


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


>>>> cProfile.run("bar.readgenome(open('cb_foo'))")
>          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 cProfile.run() or profile.run(), and timeit agrees:

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

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


-- 
Steven.




More information about the Python-list mailing list