Skip to content

Commit e8d2c80

Browse files
committed
Add some NetworkX benchmarks
1 parent 07298da commit e8d2c80

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ logging <local>
5454
mako <local>
5555
mdp <local>
5656
meteor_contest <local>
57+
networkx_shortest_path <local:networkx>
58+
networkx_connected_components <local:networkx>
59+
networkx_k_core <local:networkx>
5760
nbody <local>
5861
nqueens <local>
5962
pathlib <local>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.pyperformance]
2+
name = "networkx_connected_components"
3+
extra_opts = ["connected_components"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.pyperformance]
2+
name = "networkx_k_core"
3+
extra_opts = ["k_core"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.pyperformance]
2+
name = "networkx_shortest_path"
3+
extra_opts = ["shortest_path"]
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_networkx"
3+
requires-python = ">=3.11"
4+
dependencies = [
5+
"pyperf",
6+
"networkx",
7+
]
8+
urls.repository = "https://github.com/python/pyperformance"
9+
dynamic = ["version"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
networkx==3.4.2
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Some graph algorithm benchmarks using networkx
3+
4+
This uses the public domain Amazon data set from the SNAP benchmarks:
5+
6+
https://snap.stanford.edu/data/amazon0302.html
7+
8+
Choice of benchmarks inspired by Timothy Lin's work here:
9+
10+
https://www.timlrx.com/blog/benchmark-of-popular-graph-network-packages
11+
"""
12+
13+
import collections
14+
from pathlib import Path
15+
16+
import networkx
17+
18+
import pyperf
19+
20+
21+
DATA_FILE = Path(__file__).parent / "data" / "amazon0302.txt.gz"
22+
23+
24+
graph = networkx.read_adjlist(DATA_FILE)
25+
26+
27+
def bench_shortest_path():
28+
collections.deque(networkx.shortest_path_length(graph, "0"))
29+
30+
31+
def bench_connected_components():
32+
networkx.number_connected_components(graph)
33+
34+
35+
def bench_k_core():
36+
networkx.k_core(graph)
37+
38+
39+
BENCHMARKS = {
40+
"shortest_path": bench_shortest_path,
41+
"connected_components": bench_connected_components,
42+
"k_core": bench_k_core,
43+
}
44+
45+
46+
def add_cmdline_args(cmd, args):
47+
cmd.append(args.benchmark)
48+
49+
50+
def add_parser_args(parser):
51+
parser.add_argument("benchmark", choices=BENCHMARKS, help="Which benchmark to run.")
52+
53+
54+
if __name__ == "__main__":
55+
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
56+
runner.metadata["description"] = "NetworkX benchmark"
57+
add_parser_args(runner.argparser)
58+
args = runner.parse_args()
59+
benchmark = args.benchmark
60+
61+
runner.bench_func(args.benchmark, BENCHMARKS[args.benchmark])

0 commit comments

Comments
 (0)