Created
October 30, 2022 21:57
-
-
Save mikebenfield/3204ab7ef57f556b4aefb261cdf8f772 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 std::env; | |
use std::time::Instant; | |
use rand::{Rng, SeedableRng}; | |
use rand::rngs::StdRng; | |
use rand::distributions::{Distribution, Uniform}; | |
#[derive(Clone, Copy)] | |
enum Enum { | |
A(bool), | |
B, | |
C, | |
D, | |
E, | |
F, | |
G, | |
H, | |
} | |
struct EnumDist; | |
impl Distribution<Enum> for EnumDist { | |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Enum { | |
use Enum::*; | |
let v = Uniform::new(0u8, 7).sample(rng); | |
match v { | |
0 => A(rng.gen()), | |
1 => B, | |
2 => C, | |
3 => D, | |
4 => E, | |
5 => F, | |
6 => G, | |
7 => H, | |
_ => unreachable!(), | |
} | |
} | |
} | |
fn match_on_enum(e: Enum) -> u8 { | |
use Enum::*; | |
match e { | |
A(b) => (b as u8).wrapping_add(17), | |
B => 1, | |
C => 100, | |
D => 110, | |
E => 250, | |
F => 2, | |
G => 90, | |
H => 52, | |
} | |
} | |
fn to_time(vals: &[Enum], contents: &mut Vec<u8>) { | |
for e in vals.iter() { | |
contents.push(match_on_enum(*e)); | |
} | |
} | |
fn main() { | |
let mut rng = StdRng::seed_from_u64(101); | |
let n: usize = env::args().skip(1).next().unwrap().parse().unwrap(); | |
let data: Vec<Enum> = EnumDist.sample_iter(&mut rng).take(n).collect(); | |
let mut dump = Vec::with_capacity(n); | |
let before = Instant::now(); | |
to_time(&data, &mut dump); | |
let after = Instant::now(); | |
let dur = after.duration_since(before); | |
println!("{:?}", dur); | |
println!("{:?}", dump.last()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment