将乘法表的数字存入到一个列表中,将列表转换为字典,提高运行速度。使用字典比列表运行速度快的原因: Python 字典中使用了 hash table,因此查找操作的复杂度为 O(1),而 list 实际是个数组,在 list 中,查找需要遍历整个 list,其复杂度为 O(n),因此对成员的查找访问等操作字典要比 list 更快。
#encoding=gbk
'''
@author xupenghui
创建时间 2016年10月20
'''
from time import time
def get_multiplicationReverse():
t = time()
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data = dict.fromkeys(data, True)
#将列表转换为字典
for m in range(len(data), 0, -1):
for n in range(1, m + 1):
print(str(m) + "*" + str(n) + "=" + str(m * n) + " ", end="")
print()
print(time() - t)
pass
if __name__=='__main__':
get_multiplicationReverse()