我有两个列表,需要比较和计算元素的元素。随着这些列表越来越大,性能也越来越差。有人建议将其中一个列表分成N个部分,并并行运行比较。如何并行运行这些程序
key={}
#compare each list, element by element
for i in range(len(list1)):
for j in range(len(list2)):
matched = False
try:
matched = match_function(list[i]['typeforma'], list[i]['typeformb'],list[j]['typeforma'], list[j]['typeformb'], )
except:
print("Error",i,j)
if matched:
# store two matches in the dictionary
key[list2[j]['id']]=list1[i]['identifier']
break;
j+=1
i+=1
假设您确实需要比较笛卡尔积(} 或^{} 的
list1
中的每个元素与list2
中的每个元素,而不是只比较list1
中的每个元素与list2
中相应的元素),最简单的方法就是用对^{map
调用替换外循环唯一的诀窍是你不想尝试共享可变的
key
dict;相反,传回单独的dict并在末尾合并例如:
使用
chunksize
进行实验,但首先,有一些方法可以改进这一点。仅举一个例子,您应该直接在list1
和list2
上迭代,而不是在range(len(list1))
和range(len(list2))
上迭代—这样做不仅会使事情更简单,而且效率更高,特别是对于大的块大小。事实上,最好先简化,然后再优化相关问题 更多 >
编程相关推荐