Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use criterion::{black_box, criterion_group, criterion_main, Criterion};
- trait DoSomething {
- fn do_it(&self, i: i32) -> i32;
- }
- macro_rules! generate_action {
- ($name:ident) => {
- struct $name;
- impl DoSomething for $name {
- #[inline]
- fn do_it(&self, i: i32) -> i32 {
- i + i
- }
- }
- };
- }
- macro_rules! generate_actions {
- ($($name:ident),*) => {
- $(
- generate_action!($name);
- )*
- enum Action {
- $(
- $name($name),
- )*
- }
- fn dynamic_dispatch(actions: &Vec<Box<dyn DoSomething>>, mut output: i32) -> i32 {
- for action in actions {
- output = output + action.do_it(output);
- }
- output
- }
- fn pattern_matching(actions: &Vec<Action>, mut output: i32) -> i32 {
- for action in actions {
- match action {
- $(
- Action::$name(a) => output = output + a.do_it(output),
- )*
- }
- }
- output
- }
- fn benchmark(c: &mut Criterion) {
- let mut group = c.benchmark_group("Dispatch vs Matching");
- // Create the dynamic actions vector with all actions
- let dynamic_actions: Vec<Box<dyn DoSomething>> = vec![
- $(Box::new($name)),*
- ];
- // Create the static actions vector with all actions
- let static_actions: Vec<Action> = vec![
- $(Action::$name($name)),*
- ];
- group.bench_function("Dynamic Dispatch", |b| {
- b.iter(|| {
- let output = dynamic_dispatch(&dynamic_actions, 1);
- black_box(output)
- })
- });
- group.bench_function("Pattern Matching", |b| {
- b.iter(|| {
- let output = pattern_matching(&static_actions, 1);
- black_box(output)
- })
- });
- group.finish();
- }
- criterion_group!(benches, benchmark);
- criterion_main!(benches);
- };
- }
- // Generate 100 actions
- generate_actions!(
- ActionOne, ActionTwo, ActionThree, ActionFour, ActionFive, ActionSix, ActionSeven, ActionEight,
- ActionNine, ActionTen, ActionEleven, ActionTwelve, ActionThirteen, ActionFourteen, ActionFifteen,
- ActionSixteen, ActionSeventeen, ActionEighteen, ActionNineteen, ActionTwenty, ActionTwentyOne,
- ActionTwentyTwo, ActionTwentyThree, ActionTwentyFour, ActionTwentyFive, ActionTwentySix, ActionTwentySeven,
- ActionTwentyEight, ActionTwentyNine, ActionThirty, ActionThirtyOne, ActionThirtyTwo, ActionThirtyThree,
- ActionThirtyFour, ActionThirtyFive, ActionThirtySix, ActionThirtySeven, ActionThirtyEight, ActionThirtyNine,
- ActionForty, ActionFortyOne, ActionFortyTwo, ActionFortyThree, ActionFortyFour, ActionFortyFive,
- ActionFortySix, ActionFortySeven, ActionFortyEight, ActionFortyNine, ActionFifty, ActionFiftyOne,
- ActionFiftyTwo, ActionFiftyThree, ActionFiftyFour, ActionFiftyFive, ActionFiftySix, ActionFiftySeven,
- ActionFiftyEight, ActionFiftyNine, ActionSixty, ActionSixtyOne, ActionSixtyTwo, ActionSixtyThree,
- ActionSixtyFour, ActionSixtyFive, ActionSixtySix, ActionSixtySeven, ActionSixtyEight, ActionSixtyNine,
- ActionSeventy, ActionSeventyOne, ActionSeventyTwo, ActionSeventyThree, ActionSeventyFour, ActionSeventyFive,
- ActionSeventySix, ActionSeventySeven, ActionSeventyEight, ActionSeventyNine, ActionEighty, ActionEightyOne,
- ActionEightyTwo, ActionEightyThree, ActionEightyFour, ActionEightyFive, ActionEightySix, ActionEightySeven,
- ActionEightyEight, ActionEightyNine, ActionNinety, ActionNinetyOne, ActionNinetyTwo, ActionNinetyThree,
- ActionNinetyFour, ActionNinetyFive, ActionNinetySix, ActionNinetySeven, ActionNinetyEight, ActionNinetyNine,
- ActionOneHundred
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement