mirror of
https://github.com/bend-n/fimg.git
synced 2024-12-22 02:28:19 -06:00
use iai
This commit is contained in:
parent
c997bf1990
commit
51c506425c
11
Cargo.toml
11
Cargo.toml
|
@ -10,6 +10,17 @@ repository = "https://github.com/bend-n/fimg"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
png = { version = "0.17", features = ["unstable"], optional = true }
|
png = { version = "0.17", features = ["unstable"], optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
iai = { version = "0.1.1", features = [], git = "https://github.com/bend-n/iai/" }
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "overlays"
|
||||||
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "affine_transformations"
|
||||||
|
harness = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
save = ["png"]
|
save = ["png"]
|
||||||
default = ["save"]
|
default = ["save"]
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,20 +1,16 @@
|
||||||
#![feature(test)]
|
|
||||||
extern crate test;
|
|
||||||
use fimg::*;
|
use fimg::*;
|
||||||
use test::Bencher;
|
|
||||||
|
|
||||||
macro_rules! bench {
|
macro_rules! bench {
|
||||||
(fn $name: ident() { run $fn: ident() }) => {
|
(fn $name: ident() { run $fn: ident() }) => {
|
||||||
#[bench]
|
fn $name() {
|
||||||
fn $name(b: &mut Bencher) {
|
|
||||||
let mut img: Image<_, 4> =
|
let mut img: Image<_, 4> =
|
||||||
Image::build(64, 64).buf(include_bytes!("4_180x180.imgbuf").to_vec());
|
Image::build(160, 160).buf(include_bytes!("4_160x160.imgbuf").to_vec());
|
||||||
#[allow(unused_unsafe)]
|
for _ in 0..256 {
|
||||||
b.iter(|| unsafe {
|
#[allow(unused_unsafe)]
|
||||||
for _ in 0..256 {
|
unsafe {
|
||||||
img.$fn();
|
img.$fn()
|
||||||
}
|
};
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -24,3 +20,4 @@ bench!(fn flip_v() { run flip_v() });
|
||||||
bench!(fn rotate_90() { run rot_90() });
|
bench!(fn rotate_90() { run rot_90() });
|
||||||
bench!(fn rotate_180() { run rot_180() });
|
bench!(fn rotate_180() { run rot_180() });
|
||||||
bench!(fn rotate_270() { run rot_270() });
|
bench!(fn rotate_270() { run rot_270() });
|
||||||
|
iai::main!(flip_h, flip_v, rotate_90, rotate_180, rotate_270);
|
||||||
|
|
|
@ -1,47 +1,32 @@
|
||||||
#![feature(test)]
|
|
||||||
extern crate test;
|
|
||||||
use fimg::*;
|
use fimg::*;
|
||||||
use test::Bencher;
|
|
||||||
|
|
||||||
#[bench]
|
fn overlay_3on3at() {
|
||||||
fn overlay_3on3at(bench: &mut Bencher) {
|
let mut a: Image<_, 3> = Image::alloc(128, 128);
|
||||||
let mut v = vec![0u8; 3 * 64 * 64];
|
let b: Image<&[u8], 3> = Image::build(8, 8).buf(include_bytes!("3_8x8.imgbuf"));
|
||||||
let mut a: Image<_, 3> = Image::build(64, 64).buf(v.as_mut_slice());
|
for x in 0..16 {
|
||||||
let b: Image<&[u8], 3> = Image::build(4, 4).buf(include_bytes!("3_4x4.imgbuf"));
|
for y in 0..16 {
|
||||||
bench.iter(|| unsafe {
|
unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
|
||||||
for x in 0..16 {
|
|
||||||
for y in 0..16 {
|
|
||||||
a.overlay_at(&b, x * 4, y * 4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
assert_eq!(a.as_ref().buffer(), include_bytes!("3x3_at_out.imgbuf"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
fn overlay_4on3at() {
|
||||||
fn overlay_4on3at(bench: &mut Bencher) {
|
let mut a: Image<_, 3> = Image::alloc(128, 128);
|
||||||
let mut a: Image<_, 3> = Image::alloc(64, 64);
|
let b: Image<&[u8], 4> = Image::build(8, 8).buf(include_bytes!("4_8x8.imgbuf"));
|
||||||
let b: Image<&[u8], 4> = Image::build(4, 4).buf(include_bytes!("4_4x4.imgbuf"));
|
for x in 0..16 {
|
||||||
bench.iter(|| unsafe {
|
for y in 0..16 {
|
||||||
for x in 0..16 {
|
unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
|
||||||
for y in 0..16 {
|
|
||||||
a.as_mut().overlay_at(&b, x * 4, y * 4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
assert_eq!(a.as_ref().buffer(), include_bytes!("4x3_at_out.imgbuf"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
fn overlay_4on4at() {
|
||||||
fn overlay_4on4at(bench: &mut Bencher) {
|
let mut a: Image<_, 4> = Image::alloc(128, 128);
|
||||||
let mut a: Image<_, 4> = Image::alloc(64, 64);
|
let b: Image<&[u8], 4> = Image::build(8, 8).buf(include_bytes!("4_8x8.imgbuf"));
|
||||||
let b: Image<&[u8], 4> = Image::build(4, 4).buf(include_bytes!("4_4x4.imgbuf"));
|
for x in 0..16 {
|
||||||
bench.iter(|| unsafe {
|
for y in 0..16 {
|
||||||
for x in 0..16 {
|
unsafe { a.as_mut().overlay_at(&b, x * 8, y * 8) };
|
||||||
for y in 0..16 {
|
|
||||||
a.as_mut().overlay_at(&b, x * 4, y * 4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
assert_eq!(a.as_ref().buffer(), include_bytes!("4x4_at_out.imgbuf"));
|
|
||||||
}
|
}
|
||||||
|
iai::main!(overlay_3on3at, overlay_4on3at, overlay_4on4at);
|
||||||
|
|
|
@ -32,10 +32,14 @@ impl<B: buf::Buffer, const C: usize> Builder<B, C> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// apply a buffer, and build
|
/// apply a buffer, and build
|
||||||
|
#[track_caller]
|
||||||
pub fn buf(self, buffer: B) -> Image<B, C> {
|
pub fn buf(self, buffer: B) -> Image<B, C> {
|
||||||
if buffer.len() as u32 != C as u32 * self.width * self.height {
|
let len = C as u32 * self.width * self.height;
|
||||||
panic!("invalid buffer size");
|
assert!(
|
||||||
}
|
buffer.len() as u32 == len,
|
||||||
|
"invalid buffer size (expected {len}, got {})",
|
||||||
|
buffer.len()
|
||||||
|
);
|
||||||
Image {
|
Image {
|
||||||
buffer,
|
buffer,
|
||||||
width: self.width.try_into().expect("passed zero width to builder"),
|
width: self.width.try_into().expect("passed zero width to builder"),
|
||||||
|
|
Loading…
Reference in a new issue