Last active
September 13, 2021 06:16
-
-
Save makoConstruct/53d2bb6c433750951232aa6a701363e1 to your computer and use it in GitHub Desktop.
using criterion to benchmark copy and copy_nonoverlapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "toying" | |
version = "0.1.0" | |
edition = "2018" | |
[dependencies] | |
[dev-dependencies] | |
itertools = "0.8.2" | |
criterion = "0.3.5" | |
[[bench]] | |
name = "copy" | |
harness = false | |
#![allow(dead_code)] | |
#![feature(test)] | |
extern crate pcg; | |
extern crate rand; | |
extern crate criterion; | |
extern crate itertools; | |
// use rand::{seq::SliceRandom, Rng, SeedableRng}; | |
use criterion::{criterion_group, criterion_main, Criterion}; | |
use itertools::{Itertools}; | |
use std::{cmp::min, ptr::{copy, copy_nonoverlapping}}; | |
// fn fast_rng(seed: u64) -> impl Rng + Clone { | |
// rand::rngs::StdRng::seed_from_u64(seed) | |
// } | |
fn criterion_benchmark(c: &mut Criterion){ | |
let powers: Vec<usize> = (0..18).map(|i| 2usize.pow(i)).collect(); | |
let mut src = Vec::with_capacity(*powers.last().unwrap()); | |
let src_p:*mut usize = src.as_mut_ptr(); | |
let mut dst = Vec::with_capacity(*powers.last().unwrap()); | |
let dst_p:*mut usize = dst.as_mut_ptr(); | |
for n in powers.iter() { | |
let name = format!("copy_maybeoverlapping_{}", n); | |
c.bench_function(&name, |b|b.iter(||{ | |
unsafe{ copy(src_p, dst_p, *n); } | |
})); | |
} | |
for n in powers.iter() { | |
let name = format!("copy_nonoverlapping_{}", n); | |
c.bench_function(&name, |b|b.iter(||{ | |
unsafe{ copy_nonoverlapping(src_p, dst_p, *n); } | |
})); | |
} | |
let after = &[1,2,3,4,5,8,13,64,500,512]; | |
let clipped_powers:Vec<usize> = powers.iter().map(|i| min(*i, src.capacity() - *after.last().unwrap())).collect(); | |
for (a, n) in after.iter().cartesian_product(clipped_powers.iter()) { | |
let name = format!("copy_definitely_overlapping_after_back_{}_{}", a, n); | |
c.bench_function(&name, |b|b.iter(||{ | |
unsafe{ copy(src_p.add(*a), src_p, *n); } | |
})); | |
} | |
for (a, n) in after.iter().cartesian_product(clipped_powers.iter()) { | |
let name = format!("copy_definitely_overlapping_after_forward_{}_{}", a, n); | |
c.bench_function(&name, |b|b.iter(||{ | |
unsafe{ copy(src_p, src_p.add(*a), *n); } | |
})); | |
} | |
} | |
criterion_group!(benches, criterion_benchmark); | |
criterion_main!(benches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment