Python霧裡看花-list與set十萬資料查詢效能對比
阿新 • • 發佈:2018-11-12
# -*- coding: utf-8 -*-
import random
import time
num = 100000
listA = [random.randint(1, i) for i in range(1, num)]
setB = set()
while len(setB) < len(listA):
setB.add(random.randint(1, num))
setA = set(listA)
if __name__ == '__main__':
""" list """
t1 = time.time()
for i in xrange(0 , num):
if i in listA:
pass
print time.time() - t1
""" set 1 """
t1 = time.time()
for i in xrange(0, num):
if i in setA:
pass
print time.time() - t1
""" set 2"""
t1 = time.time()
for i in xrange(0, num):
if i in setB:
pass
print time.time() - t1
146.096999884
0.018000125885
0.0239999294281