Skip to content

Replace np.prod() with np.multiply.reduce() for better performance in shape computations #98

Description

@SaFE-APIOpt

prev_filters = int(np.prod(inshape) / np.prod(outshape[1:]))

In the line:
prev_filters = int(np.prod(inshape) / np.prod(outshape[1:]))
a more efficient alternative is:
prev_filters = int(np.multiply.reduce(inshape) / np.multiply.reduce(outshape[1:]))
np.prod() is a convenience wrapper around np.multiply.reduce() and includes extra logic such as input validation, type conversion, and parameter parsing. In performance-sensitive computations like shape transformations, using np.multiply.reduce() avoids overhead and results in a faster execution path.

This change is small but improves performance and aligns with best practices for low-level array operations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions