-
-
Save Ontonator/f10cd26d408fa2720ce54cd1f442bfb5 to your computer and use it in GitHub Desktop.
Basic benchmark.
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
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