#!/usr/bin/env python
#-*- coding:utf-8 -*- import time start = time.time()
for i in range(1000000):
a = (1, 2, 3, 4)
print time.time() - start start = time.time()
for i in range(1000000):
a = [1, 2, 3, 4]
print time.time() - start
如果要迭代一列数据,用tuple比list优。
0.107225894928
0.200721979141
结果可以看出,生成1000000数据产生的结果不一样。
test = dict((i, i) for i in range(1, 1001)) start = time.time()
for i in range(1000000):
if i in test:
pass
print time.time() - start start = time.time()
for i in range(1000000):
if test.has_key(i):
pass
print time.time() - start
另外,判断key是否存在时,用in 比 has_key优
0.151313066483
0.189944982529