Skip to content

Fastest way to convert to vec/iterate elements #1339

Closed
@RunDevelopment

Description

@RunDevelopment

Hi! I wanted to know what's the fastest way to convert a PyReadonlyArrayDyn<f32> into a Vec<f32>.

I'm making python bindings for an image processing programs, and converting numpy arrays into a contiguous memory format is the first step. I'm already using ndarray.as_slice() which allows me to skip the conversion to vec. This issue is about indexed ndarrays. I'm currently using the following code for the conversion:

let flat: Vec<f32> = ndarray.as_array().iter().cloned().collect();

Compared to a slice.to_vec(), this is more than 10x slower. Running this code on a (4320, 8468, 4) ndarray takes 0.2sec if the array is a slice (so it runs slice.to_vec()) and 3sec when the array is indexed (I cropped off 1px on the left side).

This is a huge performance cliff for me, so I would like to know what I can do to fix this.

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