如何加快代码中的瓶颈部分?线程?多重?

时间:2018-04-27 15:41:31

标签: python multithreading python-2.7 performance multiprocessing

我的代码中有一个瓶颈功能,有效归结为:

import networkx as nx
class my_class:
    def __init__(self):
        self.graph = nx.complete_graph(50)  # placeholder for large nx graph
        self.my_dict = {}

    # triggered by event automatically, cannot change trigger
    def slow_function(self, event):
        source = event.source
        dest = event.dest

        # how to speed up this function?
        def bottleneck_function():
            reasonably_fast_obj = reasonably_fast('myfoo')
            self.path_calulator(source, dest, reasonably_fast_obj)

    def path_calulator(self, source, dest, weight):
        return nx.shortest_simple_paths(self.graph, source, dest, weight)


class reasonably_fast:
    def __init__(self, foo):
        self.foo = foo

主要原因是networkx方法,它占用了大量图表的大量时间。

以上述呼叫完成之前可以再次调用slow_function(由于延迟)。什么是加速任务的适当方法?

使用多个线程可以加快速度吗?

注意:由于某些限制,我只能使用python 2.7

修改 以下是我到目前为止的情况:

import networkx as nx
from multiprocessing import Pool as ThreadPool
from itertools import islice
import random

G = nx.barabasi_albert_graph(10, 5)
G.number_of_edges()  # prints 25

def nx_function():
src, dst = random.sample(range(0,9), 2)
return list(islice(nx.shortest_simple_paths(G, source=src, target=dst), 5))

%timeit nx_function提供10000000个循环,最好为3:每循环22.1 ns

def simple():
    for i in range(10):
        nx_function()

%timeit simple给出100个循环,最好为3:每个循环1.94毫秒

def parallelized():
    pool = ThreadPool(4)
    for i in range(10):
        pool.apply_async(func=nx_function)
    pool.close()
    pool.join()

%timeit paralelized给出10个循环,最好是每循环3:196 ms

似乎多处理的开销使这无用。 还有其他方法来加速这段代码吗?

0 个答案:

没有答案