Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic networkx benchmarks #309

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mako <local>
mdp <local>
meteor_contest <local>
nbody <local>
networkx <local>
nqueens <local>
pathlib <local>
pickle <local>
Expand Down
12 changes: 12 additions & 0 deletions pyperformance/data-files/benchmarks/bm_networkx/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[project]
name = "pyperformance_bm_networkx"
requires-python = ">=3.8"
dependencies = [
"pyperf",
"networkx",
]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "networkx"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
networkx==3.1
63 changes: 63 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,63 @@
"""
Benchmark graph creation and algorithms from NetworkX.
"""

import pyperf

import networkx as nx


G = nx.erdos_renyi_graph(200, 0.02)


def bench_sparse_graph_creation():
_ = nx.erdos_renyi_graph(100, 0.02)


def bench_dense_graph_creation():
_ = nx.erdos_renyi_graph(100, 0.9)


def bench_betweenness():
_ = nx.betweenness_centrality(G)


def bench_modularity():
_ = nx.community.greedy_modularity_communities(G)


BENCHMARKS = (
"sparse_graph_creation",
"dense_graph_creation",
"betweenness",
"modularity",
)


def bench_networkx(loops, func):
t0 = pyperf.perf_counter()
for _ in range(loops):
func()
return pyperf.perf_counter() - t0


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


if __name__ == "__main__":
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
runner.metadata["description"] = "NetworkX benchmark"
runner.argparser.add_argument("benchmark", nargs="?", choices=sorted(BENCHMARKS))

args = runner.parse_args()
if args.benchmark:
benchmarks = (args.benchmark,)
else:
benchmarks = sorted(BENCHMARKS)

for bench in benchmarks:
name = "networkx_%s" % bench
func = globals()["bench_" + bench]
runner.bench_time_func(name, bench_networkx, func)
Loading