【发布时间】:2018-04-08 11:05:46
【问题描述】:
我在 Python 中编写的查询遇到了一些问题(必须将其用于 TensorFlow),该查询运行良好,但速度太慢,因为输入数据集非常大。完成查询可能需要 5 分钟以上,检查任务管理器我可以确认它确实在单核上运行。
代码如下:
# Assume words is a list of strings
for i, pair in enumerate(sorted(
((word, words.count(word)) # Map each word to itself and its count
for word in set(words)), # Set of unique words (remove duplicates)
key=lambda p: p[1], # Order by the frequency of each word
reverse=True)): # Descending order - less frequent words last
# Do stuff with each sorted pair
我在这里所做的只是获取输入列表words,去除重复项,然后根据输入文本中的频率降序对单词进行排序。
如果我要使用 PLINQ 在 C# 中编写此代码,我会这样做:
var query = words.AsParallel().Distinct()
.OrderByDescending(w => words.Count(s => s.Equals(w)))
.Select((w, i) => (w, i));
我找不到一种简单的方法来使用可能的内置库在 Python 中重写并行实现。我看到了一些关于Pool 扩展的指南,但这看起来只是并行Select 操作的等价物,所以我仍然想念如何在Python 中并行实现Distinct 和OrderByDescending 操作.
是否可以使用内置库来做到这一点,或者是否有常用的 3rd 方库来做到这一点?
谢谢!
【问题讨论】:
标签: c# python multithreading python-multithreading plinq