Method much slower than function?

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


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

> 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.


After further testing, I think I have found the cause of the speed
difference -- and it isn't that the code is a method.

Here's my test code:


def readgenome(filehandle):
    s = ""
    for line in filehandle.xreadlines():
        s += line.strip()

class SlowClass:
    def readgenome(self, filehandle):
        self.s = ""
        for line in filehandle.xreadlines():
            self.s += line.strip()

class FastClass:
    def readgenome(self, filehandle):
        s = ""
        for line in filehandle.xreadlines():
            s += line.strip()
        self.s = s


Now I test them. For brevity, I am leaving out the verbose profiling
output, and just showing the total function calls and CPU time.


>>> import cProfile
>>> cProfile.run("readgenome(open('cb_foo'))")
         20005 function calls in 0.071 CPU seconds

>>> cProfile.run("SlowClass().readgenome(open('cb_foo'))")
         20005 function calls in 4.030 CPU seconds

>>> cProfile.run("FastClass().readgenome(open('cb_foo'))")
         20005 function calls in 0.077 CPU seconds


So you can see that the slow-down for calling a method (compared to a
function) is very small.

I think what we're seeing in the SlowClass case is the "normal" speed of
repeated string concatenations. That's REALLY slow. In the function and
FastClass cases, the compiler optimization is able to optimize that slow
behaviour away.

So, nothing to do with methods vs. functions, and everything to do with
the O(N**2) behaviour of repeated string concatenation.


-- 
Steven.




More information about the Python-list mailing list