Skip to content

Conversation

@KeKsBoTer
Copy link
Contributor

Hello,

I often want to read the whole data from an array (using .into_vec()).
I have noticed that this is considerably slower for large arrays compared to numpy.

Numpy:
Screenshot from 2024-05-16 16-34-23

This crates takes 6 times as long just for reading:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("big.npy")?;

    let npy = npyz::NpyFile::new(file)?;
    let mut sum:f32 = 0.;
    let start = std::time::Instant::now();
    let data = npy.into_vec::<f32>()?;

    println!("reading took: {:?}", start.elapsed());
    
    // do something
    println!("length {:?}", data.len()  );
    

    for arr in data {
        sum+=arr;
    }
    println!("{:.4}", sum);
    Ok(())
}

output:
Screenshot from 2024-05-16 16-33-32

This boils down to the reader reading and parsing every primitive one by one.
In many cases, we can copy the data into memory and reinterpret it (this is also what Numpy does).

I added a fast read functionality for primitive types using the bytemuck crate.
This makes it about 10 times faster:
Screenshot from 2024-05-16 16-33-47

What are your thoughts about this?
My solution adds minimal code and only speeds up the reads for the primitives where it is safe.

Sorry for the convoluted git history...please squash it on merge in GitHub.

Best
Simon

@ExpHP
Copy link
Owner

ExpHP commented Jul 7, 2024

I considered adding block-reading functionality to the crate but concluded I was just reinventing NpyFile<BufReader<File>>. How does this compare to that in benchmarks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants