Skip to content

Optimization: Replace np.empty_like(a) with np.empty(a.shape, dtype=a.dtype) after pd_to_np conversion #17

@SaFE-APIOpt

Description

@SaFE-APIOpt

out = np.empty_like(a)

Hi, thank you for your great work!

In the following snippet:

a = pd_to_np(a)
out = np.empty_like(a)

after converting a pandas object to a NumPy array, the shape and dtype of a are already known and reliable. While np.empty_like(a) is valid and convenient, it incurs minor overhead due to internal type-checking and metadata copying.

A faster and equally correct alternative is:
out = np.empty(a.shape, dtype=a.dtype)
This avoids unnecessary introspection and speeds up array initialization, particularly in performance-critical loops or batch processing routines.

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