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(file handle):
s = ''
for line in filehandle.xrea dlines():
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.xrea dlines():
if '>' in line:
continue
self.s += line.strip()
############### ## END SOURCE ##############
When running the function and the method on a 20,000 line text file, I
get the following:
20004 function calls in 10.214 CPU seconds>>cProfile.run( "bar.readgenome (open('cb_foo') )")
Ordered by: standard name
ncalls tottime percall cumtime percall
filename:lineno (function)
1 0.000 0.000 10.214 10.214 <string>:1(<mod ule>)
1 10.205 10.205 10.214 10.214 reader.py:11(re adgenome)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profil er' objects}
19999 0.009 0.000 0.009 0.000 {method 'strip' of 'str'
objects}
1 0.000 0.000 0.000 0.000 {method 'xreadlines' of
'file' objects}
1 0.000 0.000 0.000 0.000 {open}
20004 function calls in 0.041 CPU seconds>>cProfile.run( "z=r.readgenome (open('cb_foo') )")
Ordered by: standard name
ncalls tottime percall cumtime percall
filename:lineno (function)
1 0.000 0.000 0.041 0.041 <string>:1(<mod ule>)
1 0.035 0.035 0.041 0.041 reader.py:2(rea dgenome)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profil er' objects}
19999 0.007 0.000 0.007 0.000 {method 'strip' of 'str'
objects}
1 0.000 0.000 0.000 0.000 {method 'xreadlines' of
'file' objects}
1 0.000 0.000 0.000 0.000 {open}
The method takes 10 seconds, the function call 0.041 seconds!
Yes, I know that I wrote the underlying code rather inefficiently, and
I can streamline it with a single file.read() call instead if an
xreadlines() + strip loop. Still, the differences in performance are
rather staggering! Any comments?
Thanks,
Iddo