Skip to content

perf: replace built-in sum() with np.sum() in pagerank and katz - #8561

Merged
dschult merged 2 commits into
networkx:mainfrom
caiwen666:perf/use_numpy_sum
Mar 12, 2026
Merged

dschult merged 2 commits into
networkx:mainfrom
caiwen666:perf/use_numpy_sum

Conversation

@caiwen666

Copy link
Copy Markdown
Contributor

This pull request makes minor improvements to two centrality algorithms in the NetworkX library by replacing the built-in sum function with NumPy's np.sum for better performance and consistency with NumPy arrays.

Algorithm consistency and performance improvements:

@dschult

dschult commented Mar 11, 2026

Copy link
Copy Markdown
Member

So... does this make an improvement in the performance of the code?

@caiwen666

Copy link
Copy Markdown
Contributor Author

So... does this make an improvement in the performance of the code?

Maybe.

I added the following code (between #begin and #end) inside the _pagerank_scipy function to measure the difference between sum and np.sum:

...
for _ in range(max_iter):
        xlast = x
        # begin
        start = time.perf_counter()
        sum1 = np.sum(x[is_dangling])
        sum_time1 = time.perf_counter() - start
        start = time.perf_counter()
        sum2 = sum(x[is_dangling])
        sum_time2 = time.perf_counter() - start
        start = time.perf_counter()
        sum3 = sum(x[is_dangling])
        sum_time3 = time.perf_counter() - start
        start = time.perf_counter()
        sum4 = np.sum(x[is_dangling])
        sum_time4 = time.perf_counter() - start
        # end
        print(f"Sum Time: {sum_time1:.10f} seconds, {sum_time2:.10f} seconds, {sum_time3:.10f} seconds, {sum_time4:.10f} seconds")
        x = alpha * (x @ A + x[is_dangling].sum() * dangling_weights) + (1 - alpha) * p
...

I then ran the following code:

pagerank = nx.pagerank(
    G,
    alpha=0.85,
    personalization=None,
    max_iter=100,
    tol=1e-06,
    nstart=None,
    dangling=None,
)

On this dataset: https://snap.stanford.edu/data/web-Google.html, I obtained:

Sum Time: 0.0007621000 seconds, 0.0040127000 seconds, 0.0037306000 seconds, 0.0005124000 seconds
Sum Time: 0.0008018000 seconds, 0.0041156000 seconds, 0.0040253000 seconds, 0.0007563000 seconds

(the first and fourth numbers are np.sum, the second and third are sum).

So np.sum is indeed noticeably faster than sum in this case.

I also tested on this dataset: https://snap.stanford.edu/data/ego-Twitter.html, and got:

Sum Time: 0.0000564000 seconds, 0.0003408000 seconds, 0.0003218000 seconds, 0.0000173000 seconds
Sum Time: 0.0000206000 seconds, 0.0003635000 seconds, 0.0003506000 seconds, 0.0000235000 seconds
Sum Time: 0.0000201000 seconds, 0.0003369000 seconds, 0.0003329000 seconds, 0.0000116000 seconds
Sum Time: 0.0000197000 seconds, 0.0002910000 seconds, 0.0002886000 seconds, 0.0000115000 seconds

Again, np.sum is consistently faster than the built-in sum.

@dschult

dschult commented Mar 11, 2026

Copy link
Copy Markdown
Member

I mean... not which sum is faster, but how much does this affect the overall time. e.g. if you time an example with the main branch and then time it with the PR how do the timings compare?

@caiwen666

Copy link
Copy Markdown
Contributor Author

I mean... not which sum is faster, but how much does this affect the overall time. e.g. if you time an example with the main branch and then time it with the PR how do the timings compare?

The sum step itself is very fast, so if the number of iterations is small, it' s hard to see a significant overall improvement.

To get a more visible comparison, I deliberately adjusted the parameters (alpha and tol). Here is the code I used:

for _ in range(10):
    start = time.perf_counter()
    pagerank = nx.pagerank(
        G,
        alpha=0.99,
        personalization=None,
        max_iter=10000,
        tol=1e-12,
        nstart=None,
        dangling=None,
    )
    elapsed = time.perf_counter() - start
    print(f"Time: {elapsed:.10f} seconds")

On the dataset at https://snap.stanford.edu/data/ego-Twitter.html, the algorithm runs for 1135 iterations.

Here are the timings:

My branch
Time: 7.8520538230 seconds
Time: 6.9454437420 seconds
Time: 6.9844150580 seconds
Time: 6.8511085700 seconds
Time: 6.9092666380 seconds
Time: 6.8833404740 seconds
Time: 6.7569255670 seconds
Time: 6.6949019480 seconds
Time: 6.8056253960 seconds
Time: 6.7760926810 seconds
Main branch
Time: 8.6930078160 seconds
Time: 7.7676038510 seconds
Time: 7.8321767780 seconds
Time: 7.7923509250 seconds
Time: 7.8181174230 seconds
Time: 7.7545174510 seconds
Time: 7.7259882450 seconds
Time: 7.6430890900 seconds
Time: 7.7058533530 seconds
Time: 7.6836238290 seconds

There is indeed a measurable performance improvement, even though it is not very large.

@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.

Seems fine to me - AFAICT everything being summed here is already a numpy array so np.sum can be used without having to worry about asarray overheads or issues with generators.

@dschult
dschult merged commit 802c0a2 into networkx:main Mar 12, 2026
40 of 41 checks passed
@dschult dschult added this to the 3.7 milestone Mar 12, 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.

4 participants