0

我正在编写在文本中并行搜索单词的程序。我在运行线程时遇到“问题”,因为它在程序中花费的时间最长。如果有更多线程,则查找单词所需的时间更少,因为文本块在线程之间划分。但我试图衡量哪个部分花费的时间最多,它是启动线程的这一部分。这是代码:

startThreadsStart=time.time()
for i in range(0,threads_number):
    threads.append(ParallelStringSearch("something", i)) 
    threads[i].start()
startThreadsEnd = time.time()-startThreadsStart

和 Thread 类中的 run() 方法:

 def run(self):
    self.time = time.time()
    self.search()
    self.end_time = time.time()-self.time
    print "EXECUTION: ",self.index,self.end_time
4

1 回答 1

1

假设您使用 CPython 来并行化 CPU 密集型任务,python 线程将无法帮助您,因为GIL。尝试使用子流程。

于 2013-03-09T09:51:33.580 回答