Skip to content

Optimization: Replace np.apply_along_axis(np.sum, 1, ...) with .sum(axis=1) for faster row-wise summation #32

@SaFE-APIOpt

Description

@SaFE-APIOpt

ip_sum = np.apply_along_axis(np.sum, 1, intv_bin_ip)

Current implementation:

ip_sum = np.apply_along_axis(np.sum, 1, intv_bin_ip)
con_sum = np.apply_along_axis(np.sum, 1, intv_bin_con)

Recommended replacement:

ip_sum = intv_bin_ip.sum(axis=1)
con_sum = intv_bin_con.sum(axis=1)

The use of np.apply_along_axis(np.sum, 1, ...) results in significant performance overhead. This function works by iterating over each row of the array in Python space and applying the np.sum function individually, which is inefficient, especially for large arrays.

By replacing it with the vectorized version .sum(axis=1), the operation is executed entirely in C, avoiding Python-level loops. This leads to much faster computation and lower memory overhead.

Both implementations are functionally identical, producing the same output, but the vectorized form is highly optimized and is the recommended practice for numerical array operations in NumPy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions