提高python性能之绑定方法

import time

class Test():
  def m(self, n):
    return n

test = Test()

t1 = 0
f = test.m
for j in xrange(10):
  sum = 0
  t = time.time()
  for i in xrange(1000000):
    sum += f(i)
  t1 += time.time() - t

t2 = 0
for j in xrange(10):
  sum = 0
  t = time.time()
  for i in xrange(1000000):
    sum += test.m(i)
  t2 += time.time() - t

print t1 /10
print t2 /10
print (t2 - t1) / t2 * 100, '%'


结果:

0.85
1.00780000687
15.6578691994 %

性能相差15%!看来python有绑定方法还是挺不错的!

posted @ 2012-08-06 11:24  网恋被骗5元  阅读(496)  评论(2编辑  收藏  举报