Skip to content

Avoid redundant np.empty(...) when initializing zero arrays β€” use np.zeros(...) directlyΒ #3

@SaFE-APIOpt

Description

@SaFE-APIOpt

accompany_new = np.zeros_like(np.empty((track_length, 128)))

Hi πŸ‘‹ I found several lines in the code like this:

rhythm_new = np.zeros_like(np.empty((track_length, 128)))
bass_new = np.zeros_like(np.empty((track_length, 128)))
accompany_new = np.zeros_like(np.empty((track_length, 128)))
melody_new = np.zeros_like(np.empty((track_length, 128)))

These can be simplified and optimized as:
rhythm_new = np.zeros((track_length, 128)) bass_new = np.zeros((track_length, 128)) accompany_new = np.zeros((track_length, 128)) melody_new = np.zeros((track_length, 128))

  • np.empty(...) creates an uninitialized array that is immediately discarded.
  • np.zeros_like(...) then allocates a second array β€” introducing unnecessary memory and computation overhead.
  • np.zeros(...) is more direct, efficient, and improves readability.

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