Skip to content

Replace reshape(-1,) with ravel() for efficient flattening #41

@SaFE-APIOpt

Description

@SaFE-APIOpt

val = np.array(var.value).reshape(-1,)

Current code:
val = np.array(var.value).reshape(-1,)
Proposed replacement:
val = np.array(var.value).ravel()
Both reshape(-1,) and ravel() flatten arrays into 1-D.

Benchmarks show that ravel() consistently outperforms reshape(-1,), because it is optimized for flattening and avoids extra shape compatibility checks.

ravel() is also more robust: it returns a view whenever possible and only falls back to a copy if required, while reshape(-1,) requires the array to be contiguous.

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