449,265 Members | 1,710 Online Need help? Post your question and get tips & solutions from a community of 449,265 IT Pros & Developers. It's quick & easy.

# Psyco performance

 P: n/a I'm not seeing much benefit from psyco (only 5-10% faster). Maybe this example is too trivial? Can someone give me some pointers as to what kind of code would see a dramatic benefit? Here's the code: import time import psyco n = 100000 t1 = time.clock() l = list(range(0,n)) l2 = [x**2 for x in l] t2 = time.clock() no_psyco = t2 - t1 psyco.log() psyco.full() t1 = time.clock() l = list(range(0,n)) l2 = [x**2 for x in l] t2 = time.clock() with_psyco = t2 - t1 print 'without psyco = ',no_psyco print 'with psyco = ',with_psyco print 'delta = ',(((no_psyco - with_psyco)/no_psyco) * 100),'%' Jun 20 '06 #1
6 Replies

 P: n/a da********@yahoo.com wrote: I'm not seeing much benefit from psyco (only 5-10% faster). Maybe this example is too trivial? Can someone give me some pointers as to what kind of code would see a dramatic benefit? Here's the code: import time import psyco n = 100000 t1 = time.clock() l = list(range(0,n)) l2 = [x**2 for x in l] t2 = time.clock() no_psyco = t2 - t1 psyco.log() psyco.full() t1 = time.clock() l = list(range(0,n)) l2 = [x**2 for x in l] t2 = time.clock() with_psyco = t2 - t1 print 'without psyco = ',no_psyco print 'with psyco = ',with_psyco print 'delta = ',(((no_psyco - with_psyco)/no_psyco) * 100),'%' 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. Jun 20 '06 #2

 P: n/a What's the reasoning behind requiring everything to be in functions? Just curious. On 6/20/06, Christophe wrote: 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. Jun 20 '06 #3

 P: n/a Hello, Gregory Piñero a écrit : What's the reasoning behind requiring everything to be in functions? Just curious. You may want to read this: http://psyco.sourceforge.net/introdu...-jit-compilers Psyco has to run the code at least once to emit code specialized for the actual data. It works by replacing blocks of code by other blocks, optimized for the kind of data seen the previous times. On the contrary, the code outside functions is run only once. You'll never get the chance to run the optimized version again... -- Amaury Jun 20 '06 #4

 P: n/a > 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 The code: import time import psyco class Test(object): def __init__(self, value): self.value = value def foo(self): return reduce(lambda x,y : x + y, list(range(0,self.value))) def test(n): l = [Test(i) for i in range(1, n)] return [x.foo() for x in l] n = 1000 t1 = time.clock() l2 = test(n) t2 = time.clock() no_psyco = t2 - t1 psyco.full() t1 = time.clock() l2 = test(n) t2 = time.clock() with_psyco = t2 - t1 print 'without psyco = ',no_psyco print 'with psyco = ',with_psyco delta = (no_psyco - with_psyco) if(delta > 0): result = 'faster' else: result = 'slower' print 'with psyco = ',abs(delta),result Jun 20 '06 #5

 P: n/a 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 Jun 21 '06 #6

 P: n/a > > > 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. Another thing I wasn't considering is that the first call with psyco enabled might be slower. The 2nd time the psyco-compiled function is called is where the speed improvement may be present. With the code at the bottom, I get these results: without psyco = 0.000421282593179 first call with psyco = 0.000902349320933 with psyco = 5.30793718196e-005 first call with psyco = 114.190981432 % slower 2nd call with psyco = 87.400530504 % faster import time import psyco def test(l): result = 0 for item in l: result += item return result l = list(range(0, 1000)) t1 = time.clock() l2 = test(l) t2 = time.clock() no_psyco = t2 - t1 psyco.log() psyco.bind(test) t1 = time.clock() l2 = test(l) t2 = time.clock() first_call_with_psyco = t2 - t1 t1 = time.clock() l2 = test(l) t2 = time.clock() with_psyco = t2 - t1 print 'without psyco = ',no_psyco print 'first call with psyco = ',first_call_with_psyco print 'with psyco = ',with_psyco first_delta = ((no_psyco - first_call_with_psyco)/no_psyco) * 100 delta = ((no_psyco - with_psyco)/no_psyco) * 100 if(first_delta > 0): result = 'faster' else: result = 'slower' print 'first call with psyco = ',abs(first_delta),'% ',result if(delta > 0): result = 'faster' else: result = 'slower' print '2nd call with psyco = ',abs(delta),'% ',result Jun 21 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion. 