Skip to content

Instantly share code, notes, and snippets.

@Ontonator
Created January 10, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ontonator/f10cd26d408fa2720ce54cd1f442bfb5 to your computer and use it in GitHub Desktop.
Save Ontonator/f10cd26d408fa2720ce54cd1f442bfb5 to your computer and use it in GitHub Desktop.
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