用Psyco来加速Python
2009 3 9 11:15 PM 2961次查看
下面来用pystone测试一下:
import psyco
import test.pystone
print 'Befor psyco'
for i in xrange(10):
test.pystone.main()
psyco.bind(test.pystone.main)
print 'After psyco'
for i in xrange(10):
test.pystone.main()
结果:Befor psyco速度提升到了5倍多!
Pystone(1.1) time for 50000 passes = 1.23694
This machine benchmarks at 40422.2 pystones/second
Pystone(1.1) time for 50000 passes = 1.16988
This machine benchmarks at 42739.3 pystones/second
Pystone(1.1) time for 50000 passes = 1.16784
This machine benchmarks at 42814 pystones/second
Pystone(1.1) time for 50000 passes = 1.16724
This machine benchmarks at 42836.1 pystones/second
Pystone(1.1) time for 50000 passes = 1.16481
This machine benchmarks at 42925.4 pystones/second
Pystone(1.1) time for 50000 passes = 1.16854
This machine benchmarks at 42788.4 pystones/second
Pystone(1.1) time for 50000 passes = 1.17375
This machine benchmarks at 42598.7 pystones/second
Pystone(1.1) time for 50000 passes = 1.17071
This machine benchmarks at 42709.1 pystones/second
Pystone(1.1) time for 50000 passes = 1.16908
This machine benchmarks at 42768.6 pystones/second
Pystone(1.1) time for 50000 passes = 1.16718
This machine benchmarks at 42838.3 pystones/second
After psyco
Pystone(1.1) time for 50000 passes = 0.203744
This machine benchmarks at 245406 pystones/second
Pystone(1.1) time for 50000 passes = 0.211578
This machine benchmarks at 236319 pystones/second
Pystone(1.1) time for 50000 passes = 0.214457
This machine benchmarks at 233146 pystones/second
Pystone(1.1) time for 50000 passes = 0.215115
This machine benchmarks at 232433 pystones/second
Pystone(1.1) time for 50000 passes = 0.212459
This machine benchmarks at 235340 pystones/second
Pystone(1.1) time for 50000 passes = 0.215476
This machine benchmarks at 232045 pystones/second
Pystone(1.1) time for 50000 passes = 0.212989
This machine benchmarks at 234753 pystones/second
Pystone(1.1) time for 50000 passes = 0.213208
This machine benchmarks at 234512 pystones/second
Pystone(1.1) time for 50000 passes = 0.215076
This machine benchmarks at 232476 pystones/second
Pystone(1.1) time for 50000 passes = 0.213314
This machine benchmarks at 234396 pystones/second
再试试循环:
import time, psyco
def f(n):
sum = 0
for i in xrange(n):
sum += n
return sum
def g(n, m):
sum = 0
for i in xrange(1, m):
sum += n ** i
return sum
print 'Befor psyco'
t = time.time()
sum = f(10000000)
if sum:
print time.time() - t
t = time.time()
sum = g(3, 10000)
if sum:
print time.time() - t
psyco.bind(f)
psyco.bind(g)
print 'After psyco'
t = time.time()
sum = f(10000000)
if sum:
print time.time() - t
t = time.time()
sum = g(3, 10000)
if sum:
print time.time() - t
结果:Befor psyco可见简单的运算提升较多,复杂的则几乎没影响。其实文档中也说到了,乘方操作并不会提速。
3.18700003624
2.375
After psyco
1.75
2.3599998951
看来用这个之前还得好好研究下是否有必要~
0条评论 你不来一发么↓