<da********@yahoo.com> wrote in message
news:11**********************@r2g2000cwb.googlegro ups.com...
Place all the code in a function. Even without psyco you might get
somewhat better performances then. And I doubt psyco can optimise code
that isn't in a function anyway.
And lastly, most of the code is probably spend computing x**2 which is
already optimised C code.
I've changed the code to include a class, method call, and function.
Now the Psyco code is quite a bit slower. Is this a valid way to test
Psyco's effects? When I run the following code I get this result:
without psyco = 0.96840101186
with psyco = 1.82430169197
with psyco = 0.855900680114 slower
Here are 3 different implementations of foo, with varying degrees of
improvement.
func without with
foo1: 0.1727 0.0106
foo2: 0.1020 0.1012
foo3: 0.3632 0.8068
foo1 is just a brute force for-loop summing the values of the composed list,
foo2 calls sum(), and foo3 is the original foo using reduce().
Surprisingly, brute force + psyco beats reduce and sum without psyco.
psyco's strength is in compiling Python code inside functions. In foo2 and
foo3, most of the processing is done not in explicit Python, but in C code
implementation of sum and reduce, so the psyco processing is actually adding
more than it is optimizing.
-- Paul
import time
import psyco
time.clock()
class Test(object):
def __init__(self, value):
self.value = value
def foo1(self):
z = 0
for i in range(self.value):
z += i
return z
def foo2(self):
return sum(list(range(0,self.value)))
def foo3(self):
return reduce(lambda x,y : x + y, list(range(0,self.value)))
def test(n,f):
l = [Test(i) for i in range(1, n)]
return [f(x) for x in l]
n = 1000
fns = (Test.foo1, Test.foo2, Test.foo3)
no_psyco = []
with_psyco = []
for fn in fns:
t1 = time.clock()
l2 = test(n,fn)
t2 = time.clock()
no_psyco.append( t2 - t1 )
psyco.full()
for fn in fns:
t1 = time.clock()
l2 = test(n,fn)
t2 = time.clock()
with_psyco.append( t2 - t1 )
for fnData in zip([f.func_name for f in fns],no_psyco,with_psyco):
print "%s: %.4f %.4f" % fnData