def app1(count): a=[] for i in range(count): for j in range(10000): a.append((i,j)) return a
def app2(count): a=[] fun_app = a.append for i in range(count): for j in range(10000): fun_app((i,j)) return a
def run(count): time1 = time.time() app1(count) time2 = time.time() print "time app1:",time2-time1 time3 = time.time() app2(count) time4 = time.time() print "time app2:",time4-time3
>>> run(10)python
time app1: 0.0699999332428app
time app2: 0.0269999504089函數
>>> run(100)spa
time app1: 0.394000053406code
time app2: 0.313999891281class
>>> run(1000)效率
time app1: 4.13199996948變量
time app2: 3.27199983597方法
說明:im
函數中對list的方法賦與臨時變量, 實測效率有所提升, 緣由有高人說說嗎