绑定方法也能提升Python不少性能

标签:Python, 性能

C++中的虚函数会稍微影响性能,Python也不例外,下面看个例子:
import time

class A():

  def f(self, n):
    return n

a = A()

t1 = 0
f = a.f
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 += a.f(i)
  t2 += time.time() - t

print t1 /10
print t2 /10
print (t2 - t1) / t2 * 100, '%'
结果:
0.85
1.00780000687
15.6578691994 %
性能相差15%之多,不过好在Python支持绑定方法。
话说Python就不能自己增加个临时变量,优化下代码吗=。=

0条评论 你不来一发么↓

    想说点什么呢?