两个list 求交集效率对比

__author__ = 'daitr'

#--coding:utf-8--

import datetime

#方法一:

#a=[2,3,4,5]

#b=[2,5,8]

#tmp = [val for val in a if val in b]

#print tmp

##[2, 5]



#方法二

#print list(set(a).intersection(set(b)))

#print list(set(a).union(set(b)))

#print list(set(b).difference(set(a))) # b中有而a中没有的



a,b= [],[]

for i in range(1,60000):

    a.append(i)



for j in range(40000,100000):

    b.append(j)

print a[-1]

print b[-1]



time1 = datetime.datetime.now()

#print time1



tmp = [val for val in a if val in b]

#print tmp



time2 = datetime.datetime.now()

#print time2

d1 = time2-time1

print d1



list(set(a).intersection(set(b)))

#print temp2



time3 = datetime.datetime.now()

#print time3

d2 = time3-time2

print d2

 

你可能感兴趣的:(list)