Skip to content

Replace numpy.ndarray.flatten(...) with numpy.ndarray.ravel(...) for performance and efficiency #216

@SaFE-APIOpt

Description

@SaFE-APIOpt

angle_list = list(numpy.ndarray.flatten(angles))

Hi 👋 I noticed that the code uses numpy.ndarray.flatten(...) to flatten arrays, for example:

angle_list = list(numpy.ndarray.flatten(angles))
While this is functionally correct, it would be more efficient to use numpy.ndarray.ravel(...) instead:
angle_list = list(numpy.ndarray.ravel(angles))
The difference is that flatten() always returns a copy of the array, which introduces unnecessary memory overhead when a copy is not needed. On the other hand, ravel() returns a view of the array when possible, making it faster and more memory-efficient, especially with large arrays.

Since the flattened array is only being used to construct a Python list and not modified in-place, ravel() is a more optimal choice here.

This change keeps the structure and style (numpy.ndarray.method(...)) consistent, while improving runtime performance and reducing memory usage.

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