Instantly share code, notes, and snippets.

@Ontonator /main.rs Secret
Created Jan 10, 2019

Embed
What would you like to do?
Basic benchmark.
use criterion::{Criterion, criterion_group, criterion_main};
#[derive(Debug, Eq, PartialEq)]
enum AOrB {
A(String),
B(String),
}
impl AOrB {
fn swap_take_mut(&mut self) {
use self::AOrB::*;
take_mut::take(self, |old| match old {
A(s) => B(s),
B(s) => A(s),
});
}
fn swap_replace_with(&mut self) {
use self::AOrB::*;
replace_with::replace_with_or_abort(self, |old| match old {
A(s) => B(s),
B(s) => A(s),
});
}
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("take_mut", |b| b.iter(|| {
let mut a_or_b = AOrB::A("Hello World!".to_owned());
a_or_b.swap_take_mut();
// assert_eq!(a_or_b, AOrB::B("Hello World!".to_owned()));
}));
c.bench_function("replace_with", |b| b.iter(|| {
let mut a_or_b = AOrB::A("Hello World!".to_owned());
a_or_b.swap_replace_with();
// assert_eq!(a_or_b, AOrB::B("Hello World!".to_owned()));
}));
}
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