在花了很多时间尝试了解多处理之后,我想出了这段代码,这是一个基准测试:
示例1:
from multiprocessing import Process
class Alter(Process):
def __init__(self, word):
Process.__init__(self)
self.word = word
self.word2 = ''
def run(self):
# Alter string + test processing speed
for i in range(80000):
self.word2 = self.word2 + self.word
if __name__=='__main__':
# Send a string to be altered
thread1 = Alter('foo')
thread2 = Alter('bar')
thread1.start()
thread2.start()
# wait for both to finish
thread1.join()
thread2.join()
print(thread1.word2)
print(thread2.word2)
这将在 2 秒内完成(多线程时间的一半)。出于好奇,我决定接下来运行这个:
示例2:
word2 = 'foo'
word3 = 'bar'
word = 'foo'
for i in range(80000):
word2 = word2 + word
word = 'bar'
for i in range(80000):
word3 = word3 + word
print(word2)
print(word3)
令我惊恐的是,这一切只用了不到半秒!
这里发生了什么?我期望多处理运行得更快 - 考虑到示例 1 是示例 2 分成两个进程,它不应该只用示例 2 一半的时间完成吗?
Update:
在考虑了克里斯的反馈之后,我添加了消耗最多处理时间的“实际”代码,并引导我考虑多重处理:
self.ListVar = [[13379+ strings],[13379+ strings],
[13379+ strings],[13379+ strings]]
for b in range(len(self.ListVar)):
self.list1 = []
self.temp = []
for n in range(len(self.ListVar[b])):
if not self.ListVar[b][n] in self.temp:
self.list1.insert(n, self.ListVar[b][n] + '(' +
str(self.ListVar[b].count(self.ListVar[b][n])) +
')')
self.temp.insert(0, self.ListVar[b][n])
self.ListVar[b] = list(self.list1)