1 | #![feature (test)] |

2 | |

3 | **extern** **crate** test; |

4 | |

5 | **use** rand::prelude::*; |

6 | **use** test::Bencher; |

7 | **use** wyhash::WyRng; |

8 | |

9 | #[bench] |

10 | **fn** shuffle_wyhash(b: &**mut** Bencher) { |

11 | **let** **mut** rng = WyRng::from_rng(thread_rng()).unwrap(); |

12 | **let** **mut** x = (`0`..`100`).collect::<Vec<**usize**>>(); |

13 | b.iter(|| { |

14 | x.shuffle(&**mut** rng); |

15 | x[`0`] |

16 | }) |

17 | } |

18 | |

19 | #[bench] |

20 | **fn** shuffle_fastrand(b: &**mut** Bencher) { |

21 | **let** **mut** rng = fastrand::Rng::new(); |

22 | **let** **mut** x = (`0`..`100`).collect::<Vec<**usize**>>(); |

23 | b.iter(|| { |

24 | rng.shuffle(&**mut** x); |

25 | x[`0`] |

26 | }) |

27 | } |

28 | |

29 | #[bench] |

30 | **fn** u8_wyhash(b: &**mut** Bencher) { |

31 | **let** **mut** rng = WyRng::from_rng(thread_rng()).unwrap(); |

32 | b.iter(|| { |

33 | **let** **mut** sum = `0u8`; |

34 | **for** _ **in** `0`..`10_000` { |

35 | sum = sum.wrapping_add(rng.gen::<**u8**>()); |

36 | } |

37 | sum |

38 | }) |

39 | } |

40 | |

41 | #[bench] |

42 | **fn** u8_fastrand(b: &**mut** Bencher) { |

43 | **let** **mut** rng = fastrand::Rng::new(); |

44 | b.iter(|| { |

45 | **let** **mut** sum = `0u8`; |

46 | **for** _ **in** `0`..`10_000` { |

47 | sum = sum.wrapping_add(rng.u8(..)); |

48 | } |

49 | sum |

50 | }) |

51 | } |

52 | |

53 | #[bench] |

54 | **fn** u32_wyhash(b: &**mut** Bencher) { |

55 | **let** **mut** rng = WyRng::from_rng(thread_rng()).unwrap(); |

56 | b.iter(|| { |

57 | **let** **mut** sum = `0u32`; |

58 | **for** _ **in** `0`..`10_000` { |

59 | sum = sum.wrapping_add(rng.gen::<**u32**>()); |

60 | } |

61 | sum |

62 | }) |

63 | } |

64 | |

65 | #[bench] |

66 | **fn** u32_fastrand(b: &**mut** Bencher) { |

67 | **let** **mut** rng = fastrand::Rng::new(); |

68 | b.iter(|| { |

69 | **let** **mut** sum = `0u32`; |

70 | **for** _ **in** `0`..`10_000` { |

71 | sum = sum.wrapping_add(rng.u32(..)); |

72 | } |

73 | sum |

74 | }) |

75 | } |

76 | |

77 | #[bench] |

78 | **fn** fill(b: &**mut** Bencher) { |

79 | **let** **mut** rng = fastrand::Rng::new(); |

80 | b.iter(|| { |

81 | *// Pick a size that isn't divisble by 8.* |

82 | **let** **mut** bytes = [`0u8`; `367`]; |

83 | rng.fill(&**mut** bytes); |

84 | bytes |

85 | }) |

86 | } |

87 | |

88 | #[bench] |

89 | **fn** fill_naive(b: &**mut** Bencher) { |

90 | **let** **mut** rng = fastrand::Rng::new(); |

91 | b.iter(|| { |

92 | **let** **mut** bytes = [`0u8`; `367`]; |

93 | **for** item **in** &**mut** bytes { |

94 | *item = rng.u8(..); |

95 | } |

96 | bytes |

97 | }) |

98 | } |

99 | |