Skip to content

Commit

Permalink
Add some NetworkX benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Oct 22, 2024
1 parent 07298da commit e8d2c80
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ logging <local>
mako <local>
mdp <local>
meteor_contest <local>
networkx_shortest_path <local:networkx>
networkx_connected_components <local:networkx>
networkx_k_core <local:networkx>
nbody <local>
nqueens <local>
pathlib <local>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.pyperformance]
name = "networkx_connected_components"
extra_opts = ["connected_components"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.pyperformance]
name = "networkx_k_core"
extra_opts = ["k_core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.pyperformance]
name = "networkx_shortest_path"
extra_opts = ["shortest_path"]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_networkx"
requires-python = ">=3.11"
dependencies = [
"pyperf",
"networkx",
]
urls.repository = "https://github.com/python/pyperformance"
dynamic = ["version"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
networkx==3.4.2
61 changes: 61 additions & 0 deletions pyperformance/data-files/benchmarks/bm_networkx/run_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Some graph algorithm benchmarks using networkx
This uses the public domain Amazon data set from the SNAP benchmarks:
https://snap.stanford.edu/data/amazon0302.html
Choice of benchmarks inspired by Timothy Lin's work here:
https://www.timlrx.com/blog/benchmark-of-popular-graph-network-packages
"""

import collections
from pathlib import Path

import networkx

import pyperf


DATA_FILE = Path(__file__).parent / "data" / "amazon0302.txt.gz"


graph = networkx.read_adjlist(DATA_FILE)


def bench_shortest_path():
collections.deque(networkx.shortest_path_length(graph, "0"))


def bench_connected_components():
networkx.number_connected_components(graph)


def bench_k_core():
networkx.k_core(graph)


BENCHMARKS = {
"shortest_path": bench_shortest_path,
"connected_components": bench_connected_components,
"k_core": bench_k_core,
}


def add_cmdline_args(cmd, args):
cmd.append(args.benchmark)


def add_parser_args(parser):
parser.add_argument("benchmark", choices=BENCHMARKS, help="Which benchmark to run.")


if __name__ == "__main__":
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
runner.metadata["description"] = "NetworkX benchmark"
add_parser_args(runner.argparser)
args = runner.parse_args()
benchmark = args.benchmark

runner.bench_func(args.benchmark, BENCHMARKS[args.benchmark])

0 comments on commit e8d2c80

Please sign in to comment.