Advertisement
Guest User

Untitled

a guest
Apr 30th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  2.  
  3. trait DoSomething {
  4. fn do_it(&self, i: i32) -> i32;
  5. }
  6.  
  7. macro_rules! generate_action {
  8. ($name:ident) => {
  9. struct $name;
  10.  
  11. impl DoSomething for $name {
  12. #[inline]
  13. fn do_it(&self, i: i32) -> i32 {
  14. i + i
  15. }
  16. }
  17. };
  18. }
  19.  
  20. macro_rules! generate_actions {
  21. ($($name:ident),*) => {
  22. $(
  23. generate_action!($name);
  24. )*
  25.  
  26. enum Action {
  27. $(
  28. $name($name),
  29. )*
  30. }
  31.  
  32. fn dynamic_dispatch(actions: &Vec<Box<dyn DoSomething>>, mut output: i32) -> i32 {
  33. for action in actions {
  34. output = output + action.do_it(output);
  35. }
  36. output
  37. }
  38.  
  39. fn pattern_matching(actions: &Vec<Action>, mut output: i32) -> i32 {
  40. for action in actions {
  41. match action {
  42. $(
  43. Action::$name(a) => output = output + a.do_it(output),
  44. )*
  45. }
  46. }
  47. output
  48. }
  49.  
  50. fn benchmark(c: &mut Criterion) {
  51. let mut group = c.benchmark_group("Dispatch vs Matching");
  52.  
  53. // Create the dynamic actions vector with all actions
  54. let dynamic_actions: Vec<Box<dyn DoSomething>> = vec![
  55. $(Box::new($name)),*
  56. ];
  57.  
  58. // Create the static actions vector with all actions
  59. let static_actions: Vec<Action> = vec![
  60. $(Action::$name($name)),*
  61. ];
  62.  
  63. group.bench_function("Dynamic Dispatch", |b| {
  64. b.iter(|| {
  65. let output = dynamic_dispatch(&dynamic_actions, 1);
  66. black_box(output)
  67. })
  68. });
  69.  
  70. group.bench_function("Pattern Matching", |b| {
  71. b.iter(|| {
  72. let output = pattern_matching(&static_actions, 1);
  73. black_box(output)
  74. })
  75. });
  76.  
  77. group.finish();
  78. }
  79.  
  80. criterion_group!(benches, benchmark);
  81. criterion_main!(benches);
  82. };
  83. }
  84.  
  85. // Generate 100 actions
  86. generate_actions!(
  87. ActionOne, ActionTwo, ActionThree, ActionFour, ActionFive, ActionSix, ActionSeven, ActionEight,
  88. ActionNine, ActionTen, ActionEleven, ActionTwelve, ActionThirteen, ActionFourteen, ActionFifteen,
  89. ActionSixteen, ActionSeventeen, ActionEighteen, ActionNineteen, ActionTwenty, ActionTwentyOne,
  90. ActionTwentyTwo, ActionTwentyThree, ActionTwentyFour, ActionTwentyFive, ActionTwentySix, ActionTwentySeven,
  91. ActionTwentyEight, ActionTwentyNine, ActionThirty, ActionThirtyOne, ActionThirtyTwo, ActionThirtyThree,
  92. ActionThirtyFour, ActionThirtyFive, ActionThirtySix, ActionThirtySeven, ActionThirtyEight, ActionThirtyNine,
  93. ActionForty, ActionFortyOne, ActionFortyTwo, ActionFortyThree, ActionFortyFour, ActionFortyFive,
  94. ActionFortySix, ActionFortySeven, ActionFortyEight, ActionFortyNine, ActionFifty, ActionFiftyOne,
  95. ActionFiftyTwo, ActionFiftyThree, ActionFiftyFour, ActionFiftyFive, ActionFiftySix, ActionFiftySeven,
  96. ActionFiftyEight, ActionFiftyNine, ActionSixty, ActionSixtyOne, ActionSixtyTwo, ActionSixtyThree,
  97. ActionSixtyFour, ActionSixtyFive, ActionSixtySix, ActionSixtySeven, ActionSixtyEight, ActionSixtyNine,
  98. ActionSeventy, ActionSeventyOne, ActionSeventyTwo, ActionSeventyThree, ActionSeventyFour, ActionSeventyFive,
  99. ActionSeventySix, ActionSeventySeven, ActionSeventyEight, ActionSeventyNine, ActionEighty, ActionEightyOne,
  100. ActionEightyTwo, ActionEightyThree, ActionEightyFour, ActionEightyFive, ActionEightySix, ActionEightySeven,
  101. ActionEightyEight, ActionEightyNine, ActionNinety, ActionNinetyOne, ActionNinetyTwo, ActionNinetyThree,
  102. ActionNinetyFour, ActionNinetyFive, ActionNinetySix, ActionNinetySeven, ActionNinetyEight, ActionNinetyNine,
  103. ActionOneHundred
  104. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement