-
-
Save Ontonator/e29c8c88ee33b730a778a6e0e3b023fb to your computer and use it in GitHub Desktop.
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| { | |
let mut a_or_b = AOrB::A("Hello World!".to_owned()); | |
b.iter(|| a_or_b.swap_take_mut()); | |
}); | |
c.bench_function("replace_with", |b| { | |
let mut a_or_b = AOrB::A("Hello World!".to_owned()); | |
b.iter(|| a_or_b.swap_replace_with()); | |
}); | |
} | |
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