Skip to content

Commit 75bedd8

Browse files
committed
Extend the benchmark app to gen/save random floats
1 parent 18ab0ba commit 75bedd8

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

extras/simple-bench/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ edition = "2018"
88
fast-float = { path = "../.." }
99
structopt = "0.3"
1010
anyhow = "1.0"
11+
lexical = "5.2"
1112
lexical-core = "0.7"
13+
fastrand = "1.4"

extras/simple-bench/src/main.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
mod random;
22

3-
use std::fs::read_to_string;
3+
use std::fs;
4+
use std::iter;
45
use std::path::PathBuf;
56
use std::str::FromStr;
67
use std::time::Instant;
78

8-
use lexical_core::FromLexical;
9+
use fastrand::Rng;
10+
use lexical::FromLexical;
911
use structopt::StructOpt;
1012

1113
use fast_float::Float;
@@ -49,12 +51,12 @@ enum Cmd {
4951
/// Number of random floats generated
5052
#[structopt(short, default_value = "100000")]
5153
number: usize,
52-
/// Consise random float strings (if not, 17 digits are used)
53-
#[structopt(short)]
54-
concise: bool,
5554
/// Random generator seed
56-
#[structopt(short)]
55+
#[structopt(short, default_value = "0")]
5756
seed: u64,
57+
/// Also save the generated inputs to file
58+
#[structopt(short = "f", parse(from_os_str))]
59+
filename: Option<PathBuf>,
5860
},
5961
}
6062

@@ -163,16 +165,28 @@ fn main() {
163165
let opt: Opt = StructOpt::from_args();
164166
let (inputs, inputs_name) = match opt.command {
165167
Cmd::File { filename } => (
166-
read_to_string(&filename)
168+
fs::read_to_string(&filename)
167169
.unwrap()
168170
.trim()
169171
.lines()
170172
.map(String::from)
171173
.collect::<Vec<_>>(),
172174
filename.to_str().unwrap().to_owned(),
173175
),
174-
_ => {
175-
unimplemented!()
176+
Cmd::Random {
177+
gen,
178+
number,
179+
seed,
180+
filename,
181+
} => {
182+
let mut rng = Rng::with_seed(seed);
183+
let inputs: Vec<String> = iter::repeat_with(|| gen.gen(&mut rng))
184+
.take(number)
185+
.collect();
186+
if let Some(filename) = filename {
187+
fs::write(filename, inputs.join("\n")).unwrap();
188+
}
189+
(inputs, format!("{}", gen))
176190
}
177191
};
178192
let repeat = opt.repeat.max(1);

extras/simple-bench/src/random.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::fmt::{self, Display};
12
use std::str::FromStr;
23

34
use anyhow::{bail, Error, Result};
5+
use fastrand::Rng;
46

57
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
68
pub enum RandomGen {
@@ -14,6 +16,21 @@ pub enum RandomGen {
1416
BigInts,
1517
}
1618

19+
impl Display for RandomGen {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
match self {
22+
Self::Uniform => write!(f, "uniform"),
23+
Self::OneOverRand32 => write!(f, "one_over_rand32"),
24+
Self::SimpleUniform32 => write!(f, "simple_uniform32"),
25+
Self::SimpleInt32 => write!(f, "simple_int32"),
26+
Self::IntEInt => write!(f, "int_e_int"),
27+
Self::SimpleInt64 => write!(f, "simple_int64"),
28+
Self::BigIntDotInt => write!(f, "bigint_int_dot_int"),
29+
Self::BigInts => write!(f, "big_ints"),
30+
}
31+
}
32+
}
33+
1734
impl FromStr for RandomGen {
1835
type Err = Error;
1936

@@ -45,4 +62,24 @@ impl RandomGen {
4562
"big_ints",
4663
]
4764
}
65+
66+
pub fn gen(&self, rng: &mut Rng) -> String {
67+
match self {
68+
Self::Uniform
69+
| Self::OneOverRand32
70+
| Self::SimpleUniform32
71+
| Self::SimpleInt32
72+
| Self::SimpleInt64 => lexical::to_string(match self {
73+
Self::Uniform => rng.f64(),
74+
Self::OneOverRand32 => 1. / rng.u32(1..) as f64,
75+
Self::SimpleUniform32 => rng.u32(..) as f64 / u32::MAX as f64,
76+
Self::SimpleInt32 => rng.u32(..) as f64,
77+
Self::SimpleInt64 => rng.u64(..) as f64,
78+
_ => unreachable!(),
79+
}),
80+
Self::IntEInt => format!("{}e{}", rng.u32(..), rng.u32(..99)),
81+
Self::BigInts => format!("{}{}{}", rng.u64(..), rng.u64(..), rng.u64(..)),
82+
Self::BigIntDotInt => format!("{}.{}", rng.u32(..), rng.u32(..)),
83+
}
84+
}
4885
}

0 commit comments

Comments
 (0)