Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2019 18:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Code shared from the Rust Playground
extern crate criterion;
// Don't run this on Playground!
// Run it locally using `cargo bench`
use criterion::Criterion;
use ndarray::Array1;
fn midpoints(n: u32, a: f32, b: f32) -> Array1<f32> {
assert!(a < b);
assert!(n > 0);
let length = (b - a) / (n as f32);
let start = a + length;
let end = b + length;
Array1::range(start, end, length)
fn rectangle_rule<F>(n: u32, a: f32, b: f32, f: F) -> f32
F: FnMut(&f32) -> f32
let points = midpoints(n, a, b);
let f_values =;
let length = (b - a) / (n as f32);
let integral_estimate = f_values.sum() * length;
fn criterion_benchmark(c: &mut Criterion) {
move |b| b.iter(
|| rectangle_rule(50000, 0., 1., |x| x.sin())
criterion_group!(benches, criterion_benchmark);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment