Skip to content

Commit

Permalink
Introduce benchmarks for npy/npz readers
Browse files Browse the repository at this point in the history
  • Loading branch information
garyttierney committed Aug 16, 2024
1 parent 6f196ed commit 36a0449
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions hftbacktest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ hftbacktest-derive = { path = "../hftbacktest-derive", optional = true, version
[dev-dependencies]
tracing-subscriber = { version = "0.3.18", features = [] }
clap = { version = "4.5.4", features = ["derive"] }
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "formats"
harness = false

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion hftbacktest/src/backtest/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
slice::SliceIndex,
};

pub use npy::{read_npy_file, read_npz_file, write_npy, Field, NpyDTyped, NpyHeader};
pub use npy::{read_npy_file, read_npz_file, write_npy, write_npz, Field, NpyDTyped, NpyHeader};
pub use reader::{Cache, DataSource, Reader};

use crate::utils::{AlignedArray, CACHE_LINE_SIZE};
Expand Down
14 changes: 13 additions & 1 deletion hftbacktest/src/backtest/data/npy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{
fs::File,
io::{Error, ErrorKind, Read, Write},
io::{Error, ErrorKind, Read, Seek, Write},
};

use zip::{write::FileOptions, ZipWriter};

use crate::backtest::data::{npy::parser::Value, Data, DataPtr, POD};

mod parser;
Expand Down Expand Up @@ -255,6 +257,16 @@ pub fn write_npy<W: Write, T: NpyDTyped>(write: &mut W, data: &[T]) -> std::io::
Ok(())
}

pub fn write_npz<W: Write + Seek, T: NpyDTyped>(write: &mut W, data: &[T]) -> std::io::Result<()> {
let mut archive = ZipWriter::new(write);

archive.start_file::<_, (), _>("data.npy", FileOptions::default())?;
write_npy(&mut archive, data)?;
archive.finish()?;

Ok(())
}

fn vec_as_bytes<T>(vec: &[T]) -> &[u8] {
let len = std::mem::size_of_val(vec);
let ptr = vec.as_ptr() as *const u8;
Expand Down

0 comments on commit 36a0449

Please sign in to comment.