-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.py
54 lines (43 loc) · 1.7 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import numpy as np
import networkx as nx
from graph_tool.generation import complete_graph
from pyedmond._core import build_graph, minimum_branching
def make_graph(n=100, graph_type='graph_tool'):
if graph_type == 'graph_tool':
g = complete_graph(n, directed=True)
weights = np.abs(np.random.rand(g.num_edges()))
return g, weights
elif graph_type == 'networkx':
g = nx.complete_graph(n, create_using=nx.DiGraph())
weights = np.abs(np.random.rand(g.number_of_edges()))
for k, (i, j) in enumerate(g.edges_iter()):
g[i][j]['weight'] = weights[k]
return g
def test_pyedmond(n):
"""return the number of seconds required to run the algorithm
"""
g, weights = make_graph(n, 'graph_tool')
edge_and_weights = [(e[0], e[1], w)
for e, w in zip(g.get_edges(), weights)]
g = build_graph(g.num_vertices(), edge_and_weights)
s = time.time()
minimum_branching(g, [])
return time.time() - s
def test_networkx(n):
"""return the number of seconds required to run the algorithm
"""
g = make_graph(n, 'networkx')
s = time.time()
nx.maximum_spanning_arborescence(g, attr='weight', default=1)
return time.time() - s
if __name__ == '__main__':
n = 100
r = 5
pyedmond_time = np.mean([test_pyedmond(n) for i in range(r)])
networkx_time = np.mean([test_networkx(n) for i in range(r)])
print('#nodes: {}'.format(n))
print('repetition: {}'.format(5))
print('pyedmond takes {} secs on average'.format(pyedmond_time))
print('networkx takes {} secs on average'.format(networkx_time))
print('pyedmond is {} times faster'.format(networkx_time / pyedmond_time))