Last active
August 5, 2018 19:37
-
-
Save Pazzaz/91aa3c475ead8096daae2f0d7af7574b to your computer and use it in GitHub Desktop.
code to benchmark sampling from a uniform range of durations
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
import plotly.offline as py | |
import plotly.graph_objs as go | |
file_1 = 'final_old' | |
file_2 = 'final_new' | |
hist_name = 'final_histogram' | |
lines = [line.rstrip('\n').split(',') for line in open(file_1)] | |
old_data = [] | |
current = -1 | |
for line in lines: | |
x = int(line[0]) | |
if x > current: | |
current = x | |
old_data.append([]) | |
old_data[-1].append(float(line[2])) | |
lines = [line.rstrip('\n').split(',') for line in open(file_2)] | |
new_data = [] | |
current = -1 | |
for line in lines: | |
x = int(line[0]) | |
if x > current: | |
current = x | |
new_data.append([]) | |
new_data[-1].append(float(line[2])) | |
layout = go.Layout(yaxis=dict(scaleanchor="x", scaleratio=1)) | |
trace = go.Heatmap(z=old_data, colorscale='Viridis', zmin=20, zmax=80, transpose = True) | |
data = go.Figure(data=[trace], layout=layout) | |
py.plot(data, filename=file_1 + '.html') | |
trace = go.Heatmap(z=new_data, colorscale='Viridis', zmin=20, zmax=80, transpose = True) | |
data = go.Figure(data=[trace], layout=layout) | |
py.plot(data, filename=file_2 + '.html') | |
diff = [[1 - (a / b) for a, b in zip(i, j)] for i, j in zip(new_data, old_data)] | |
data = [go.Histogram(x=[item for sublist in diff for item in sublist])] | |
py.plot(data, filename=hist_name + '.html') |
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
#![feature(duration_as_u128)] | |
extern crate rand; | |
use rand::distributions::Uniform; | |
use rand::{thread_rng, Rng}; | |
use std::time::{Duration, Instant}; | |
const MAX_NANOS: u128 = u64::max_value() as u128 + 1_000_000_000; | |
const TESTING_DURATIONS: usize = 800; | |
const STEP_SIZE: usize = (MAX_NANOS / TESTING_DURATIONS as u128) as usize; | |
fn main() { | |
let mut values = Vec::with_capacity(100); | |
let mut rng = thread_rng(); | |
let mut durations = Vec::with_capacity(TESTING_DURATIONS); | |
for higher_n in (0..u128::max_value()).step_by(STEP_SIZE) { | |
if higher_n >= MAX_NANOS { | |
break; | |
} | |
let dur = Duration::new( | |
(higher_n / 1_000_000_000) as u64, | |
(higher_n % 1_000_000_000) as u32, | |
); | |
durations.push(dur); | |
} | |
// Warmup | |
for (x, high) in durations.iter().cloned().enumerate().step_by(10) { | |
for (y, low) in durations.iter().cloned().enumerate().step_by(10) { | |
if low > high { | |
break | |
} | |
let uniform = Uniform::new_inclusive(low, high); | |
values.clear(); | |
for _ in 0..1000 { | |
let before = Instant::now(); | |
{ | |
let a = rng.sample(uniform); | |
} | |
let after = Instant::now(); | |
let time_between = after.duration_since(before); | |
values.push(time_between); | |
} | |
let average = values.iter().map(|x|x.as_nanos()).sum::<u128>() as f64 / 1000.0; | |
} | |
} | |
// Real bench | |
for (x, high) in durations.iter().cloned().enumerate() { | |
for (y, low) in durations.iter().cloned().enumerate() { | |
if low > high { | |
break | |
} | |
values.clear(); | |
let uniform = Uniform::new_inclusive(low, high); | |
for _ in 0..1000 { | |
let before = Instant::now(); | |
{ | |
let a = rng.sample(uniform); | |
} | |
let after = Instant::now(); | |
let time_between = after.duration_since(before); | |
values.push(time_between); | |
} | |
let average = values.iter().map(|x|x.as_nanos()).sum::<u128>() as f64 / 1000.0; | |
println!("{},{},{}", x, y, average); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment