In python, I wonder something.(Apologize my english) I wonder intersection speed between set and list
There is set A, set B and list C, list D. set A and list C, there is elements like 'lion' 'tiger' 'cat'. set B and list D, there is elements like 'lion' 'monkey' 'cat'.
I want to know intersection speed(A & B, C & D)(result: lion cat). operation A & B is faster than double for loop C and D???
Here is a way to compare several code snippets on time performance. Note that the actual execution times are system dependent, but the ratio of the performances should be approximately equal on different machines.
import timeit
setup = '''\
A = {'dog', 'cat', 'horse'}
B = {'monkey', 'horse', 'dog'}
'''
statement = '''\
intersection = A & B
'''
timeit.timeit(stmt=statement, setup=setup, number=100000)
# 0.0126 sec
setup2 = '''\
C = ['dog', 'cat', 'horse']
D = ['monkey', 'horse', 'dog']
'''
statement2 = '''\
intersection = C[:]
for i in D:
if i not in intersection:
intersection.append(i)
'''
timeit.timeit(stmt=statement2, setup=setup2, number=100000)
# 0.0312 sec
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.