Closed SheepTester closed 1 year ago
extraneous_requisites
nx.find_cycle(self.graph)
takes 14% of its execution time. Can we skip this check? Does nx.weakly_connected_components
throw an error if the graph is cyclical?
Queue
also appears slow, but this might just be a microoptimization.
Line | Time per hit |
---|---|
queue: Queue[int] = Queue() |
10.4 |
queue.put(neighbor) |
3.1 |
x = queue.get() |
3.1 |
self.graph.neighbors(x) (reference) |
0.7 |
In addition to nx.weakly_connected_components
, nx.has_path
is also relatively slow (43.1% of time, 11.2e-6 s per hit). But while a better graph algorithm could be devised, I think it's fine if I leave this alone for now.
longest_paths
, all_paths
25.8% and 36.9% of the time is also spent on nx.find_cycle(self.graph)
in longest_paths
and all_paths
, respectively.
Like extraneous_requisites
, Queue
is also used in all_paths
and is relatively slow.
g.in_edges(x[0])
and g.in_degree(u)
are relatively slow as well, but that might be inevitable.
deque
instead of Queue
all_paths(self.graph)
This recalculates
all_paths(self.graph)
for every vertex in the graph, even though it should be the same for each of them.I didn't measure longest paths in the same test, but in a separate test,