Skip to content

Commit

Permalink
Add bits benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Sep 27, 2022
1 parent d4f7343 commit e7e7a9f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions benches/deku.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuBits {
#[deku(bits = "1")]
data_01: u8,
#[deku(bits = "7")]
data_02: u8,
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuByte {
data: u8,
Expand All @@ -20,6 +28,14 @@ struct DekuVec {
data: Vec<u8>,
}

fn deku_read_bits(input: &[u8]) {
let (_rest, _v) = DekuBits::from_bytes((input, 0)).unwrap();
}

fn deku_write_bits(input: &DekuBits) {
let _v = input.to_bytes().unwrap();
}

fn deku_read_byte(input: &[u8]) {
let (_rest, _v) = DekuByte::from_bytes((input, 0)).unwrap();
}
Expand Down Expand Up @@ -51,6 +67,12 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("deku_write_byte", |b| {
b.iter(|| deku_write_byte(black_box(&DekuByte { data: 0x01 })))
});
c.bench_function("deku_read_bits", |b| {
b.iter(|| deku_read_bits(black_box([0xf1].as_ref())))
});
c.bench_function("deku_write_bits", |b| {
b.iter(|| deku_write_bits(black_box(&DekuBits { data_01: 0x0f, data_02: 0x01 })))
});

c.bench_function("deku_read_enum", |b| {
b.iter(|| deku_read_enum(black_box([0x01, 0x02].as_ref())))
Expand Down

0 comments on commit e7e7a9f

Please sign in to comment.