use bitstream_io::{BigEndian, BitRead, BitReader, BitWrite, BitWriter}; use tiny_bitstream::{constants::CTNR_BYTES_SIZE, BitDstream, BitEstream}; use criterion::{criterion_group, criterion_main, Criterion}; use rand::Rng; use std::io::Cursor; fn criterion_benchmark(c: &mut Criterion) { let mut rng = rand::thread_rng(); let len = 10_000; let mut buffer: Vec = Vec::with_capacity(len); for _ in 0..len { buffer.push(rng.gen()); } const BYTE_LEN: u8 = 8; c.bench_function("internal", |b| { b.iter(|| { let mut e_stream = BitEstream::new(); for (i, byte) in buffer.iter().enumerate() { if i > 0 && i % CTNR_BYTES_SIZE == 0 { e_stream.flush_bits(); } e_stream.add_bits(*byte as usize, BYTE_LEN).unwrap(); } e_stream.close_stream().unwrap(); let mut d_stream = BitDstream::try_from(e_stream).unwrap(); d_stream.read_bits(1).unwrap(); d_stream.reload_container().unwrap(); for i in 0..buffer.len() { if i > 0 && i % CTNR_BYTES_SIZE == 0 { d_stream.reload_container().unwrap(); } d_stream.read_bits(BYTE_LEN).unwrap(); } }) }); let mut flac: Vec = Vec::new(); c.bench_function("external", |b| { b.iter(|| { let mut writer = BitWriter::endian(&mut flac, BigEndian); for byte in buffer.iter() { writer.write(BYTE_LEN as u32, *byte).unwrap(); } let mut cursor = Cursor::new(&flac); let mut reader = BitReader::endian(&mut cursor, BigEndian); for _ in 0..buffer.len() { let _: u8 = reader.read(BYTE_LEN as u32).unwrap(); } }) }); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);