Skip to content

Simplifying _build_paths_from_predecessors - 10% faster - #8844

Merged
rossbar merged 1 commit into
networkx:mainfrom
amcandio:all_shortest_paths_refactoring
Aug 13, 2026
Merged

rossbar merged 1 commit into
networkx:mainfrom
amcandio:all_shortest_paths_refactoring

Conversation

@amcandio

Copy link
Copy Markdown
Contributor

Follow up of #8460. I remembered I wanted to simplify this one using @rossbar 's favorite trick of dict's order preservation. Code is cleaner and 10% faster :)

[75.00%] ··· benchmark_shortest_path.AllShortestPathsGrid.time_all_shortest_paths                                                                                                                                  ok
[75.00%] ··· ==== =============
              N                
             ---- -------------
              8    3.77±0.04ms 
              10    54.2±0.4ms 
              12     809±7ms   
             ==== =============

[75.00%] · For networkx commit 30bfe1b2 <main> (round 2/2):
[75.00%] ·· Building for virtualenv-py3.14...
[75.00%] ·· Benchmarking virtualenv-py3.14
[100.00%] ··· benchmark_shortest_path.AllShortestPathsGrid.time_all_shortest_paths                                                                                                                                  ok
[100.00%] ··· ==== =============
               N                
              ---- -------------
               8    4.24±0.04ms 
               10    62.2±0.6ms 
               12     906±4ms   
              ==== =============

| Change   | Before [30bfe1b2] <main>   | After [3dbc09f4] <all_shortest_paths_refactoring>   |   Ratio | Benchmark (Parameter)                                                    |
|----------|----------------------------|-----------------------------------------------------|---------|--------------------------------------------------------------------------|
| -        | 906±4ms                    | 809±7ms                                             |    0.89 | benchmark_shortest_path.AllShortestPathsGrid.time_all_shortest_paths(12) |
| -        | 4.24±0.04ms                | 3.77±0.04ms                                         |    0.89 | benchmark_shortest_path.AllShortestPathsGrid.time_all_shortest_paths(8)  |
| -        | 62.2±0.6ms                 | 54.2±0.4ms                                          |    0.87 | benchmark_shortest_path.AllShortestPathsGrid.time_all_shortest_paths(10) |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Niiiice!! Go dicts!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! thanks for this @amcandio !

@rossbar
rossbar merged commit 7f7afab into networkx:main Aug 13, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants