Skip to content

Instantly share code, notes, and snippets.

@matthieu-m
Created February 15, 2019 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieu-m/3a9ab9afc4eee80565c8d95f94db6fa4 to your computer and use it in GitHub Desktop.
Save matthieu-m/3a9ab9afc4eee80565c8d95f94db6fa4 to your computer and use it in GitHub Desktop.
use criterion::{black_box, Criterion, criterion_group, criterion_main};
use range_inclusive::FixedRangeInclusive;
fn inclusive<Idx>(start: Idx, end: Idx) -> FixedRangeInclusive<Idx> {
FixedRangeInclusive::new(start, end)
}
fn sum_exclusive(n: u64) -> u64 {
(0..(n+1)).sum()
}
fn sum_chain(n: u64) -> u64 {
(0..n).chain(::std::iter::once(n)).sum()
}
fn sum_inclusive(n: u64) -> u64 {
(0..=n).sum()
}
fn sum_fixed_inclusive(n: u64) -> u64 {
inclusive(0, n).sum()
}
fn criterion_sum(c: &mut Criterion) {
// c.bench_function("sum exclusive 1,000,000", |b| b.iter(|| sum_exclusive(1_000_000)));
// c.bench_function("sum chain 1,000,000", |b| b.iter(|| sum_chain(1_000_000)));
// c.bench_function("sum inclusive 1,000,000", |b| b.iter(|| sum_inclusive(1_000_000)));
// c.bench_function("sum fixed inclusive 1,000,000", |b| b.iter(|| sum_fixed_inclusive(1_000_000)));
c.bench_function("sum exclusive bb 1,000,000", |b| b.iter(|| sum_exclusive(black_box(1_000_000))));
c.bench_function("sum chain bb 1,000,000", |b| b.iter(|| sum_chain(black_box(1_000_000))));
c.bench_function("sum inclusive bb 1,000,000", |b| b.iter(|| sum_inclusive(black_box(1_000_000))));
c.bench_function("sum fixed inclusive bb 1,000,000", |b| b.iter(|| sum_fixed_inclusive(black_box(1_000_000))));
}
criterion_group!(sum, criterion_sum);
fn triangle_foreach_exclusive(n: u64) -> u64 {
let mut count = 0;
(0..(n + 1)).for_each(|j| count += j);
count
}
fn triangle_foreach_chain(n: u64) -> u64 {
let mut count = 0;
(0..n).chain(::std::iter::once(n)).for_each(|j| count += j);
count
}
fn triangle_foreach_inclusive(n: u64) -> u64 {
let mut count = 0;
(0..=n).for_each(|j| count += j);
count
}
fn triangle_foreach_fixed_inclusive(n: u64) -> u64 {
let mut count = 0;
inclusive(0, n).for_each(|j| count += j);
count
}
fn criterion_triangle_foreach(c: &mut Criterion) {
// c.bench_function("triangle foreach exclusive 1,000,000", |b| b.iter(|| triangle_foreach_exclusive(1_000_000)));
// c.bench_function("triangle foreach chain 1,000,000", |b| b.iter(|| triangle_foreach_chain(1_000_000)));
// c.bench_function("triangle foreach inclusive 1,000,000", |b| b.iter(|| triangle_foreach_inclusive(1_000_000)));
// c.bench_function("triangle foreach fixed inclusive 1,000,000", |b| b.iter(|| triangle_foreach_fixed_inclusive(1_000_000)));
c.bench_function("triangle foreach exclusive bb 1,000,000", |b| b.iter(|| triangle_foreach_exclusive(black_box(1_000_000))));
c.bench_function("triangle foreach chain bb 1,000,000", |b| b.iter(|| triangle_foreach_chain(black_box(1_000_000))));
c.bench_function("triangle foreach inclusive bb 1,000,000", |b| b.iter(|| triangle_foreach_inclusive(black_box(1_000_000))));
c.bench_function("triangle foreach fixed inclusive bb 1,000,000", |b| b.iter(|| triangle_foreach_fixed_inclusive(black_box(1_000_000))));
}
criterion_group!(triangle_foreach, criterion_triangle_foreach);
fn triangle_loop_exclusive(n: u64) -> u64 {
let mut count = 0;
for j in 0..(n + 1) {
count += j;
}
count
}
fn triangle_loop_chain(n: u64) -> u64 {
let mut count = 0;
for j in (0..n).chain(::std::iter::once(n)) {
count += j;
}
count
}
fn triangle_loop_inclusive(n: u64) -> u64 {
let mut count = 0;
for j in 0..=n {
count += j;
}
count
}
fn triangle_loop_fixed_inclusive(n: u64) -> u64 {
let mut count = 0;
for j in inclusive(0, n) {
count += j;
}
count
}
fn criterion_triangle_loop(c: &mut Criterion) {
// c.bench_function("triangle loop exclusive 1,000,000", |b| b.iter(|| triangle_loop_exclusive(1_000_000)));
// c.bench_function("triangle loop chain 1,000,000", |b| b.iter(|| triangle_loop_chain(1_000_000)));
// c.bench_function("triangle loop inclusive 1,000,000", |b| b.iter(|| triangle_loop_inclusive(1_000_000)));
// c.bench_function("triangle loop fixed inclusive 1,000,000", |b| b.iter(|| triangle_loop_fixed_inclusive(1_000_000)));
c.bench_function("triangle loop exclusive bb 1,000,000", |b| b.iter(|| triangle_loop_exclusive(black_box(1_000_000))));
c.bench_function("triangle loop chain bb 1,000,000", |b| b.iter(|| triangle_loop_chain(black_box(1_000_000))));
c.bench_function("triangle loop inclusive bb 1,000,000", |b| b.iter(|| triangle_loop_inclusive(black_box(1_000_000))));
c.bench_function("triangle loop fixed inclusive bb 1,000,000", |b| b.iter(|| triangle_loop_fixed_inclusive(black_box(1_000_000))));
}
criterion_group!(triangle_loop, criterion_triangle_loop);
fn addmul_foreach_exclusive(n: u64) -> u64 {
let mut count = 0;
(0..(n+1)).for_each(|j| count = (count + 3) * j);
count
}
fn addmul_foreach_chain(n: u64) -> u64 {
let mut count = 0;
(0..n).chain(::std::iter::once(n)).for_each(|j| count = (count + 3) * j);
count
}
fn addmul_foreach_inclusive(n: u64) -> u64 {
let mut count = 0;
(0..=n).for_each(|j| count = (count + 3) * j);
count
}
fn addmul_foreach_fixed_inclusive(n: u64) -> u64 {
let mut count = 0;
inclusive(0, n).for_each(|j| count = (count + 3) * j);
count
}
fn criterion_addmul_foreach(c: &mut Criterion) {
// c.bench_function("addmul foreach exclusive 1,000,000", |b| b.iter(|| addmul_foreach_exclusive(1_000_000)));
// c.bench_function("addmul foreach chain 1,000,000", |b| b.iter(|| addmul_foreach_chain(1_000_000)));
// c.bench_function("addmul foreach inclusive 1,000,000", |b| b.iter(|| addmul_foreach_inclusive(1_000_000)));
// c.bench_function("addmul foreach fixed inclusive 1,000,000", |b| b.iter(|| addmul_foreach_fixed_inclusive(1_000_000)));
c.bench_function("addmul foreach exclusive bb 1,000,000", |b| b.iter(|| addmul_foreach_exclusive(black_box(1_000_000))));
c.bench_function("addmul foreach chain bb 1,000,000", |b| b.iter(|| addmul_foreach_chain(black_box(1_000_000))));
c.bench_function("addmul foreach inclusive bb 1,000,000", |b| b.iter(|| addmul_foreach_inclusive(black_box(1_000_000))));
c.bench_function("addmul foreach fixed inclusive bb 1,000,000", |b| b.iter(|| addmul_foreach_fixed_inclusive(black_box(1_000_000))));
}
criterion_group!(addmul_foreach, criterion_addmul_foreach);
fn addmul_loop_exclusive(n: u64) -> u64 {
let mut count = 0;
for j in 0..(n+1) {
count = (count + 3) * j;
}
count
}
fn addmul_loop_chain(n: u64) -> u64 {
let mut count = 0;
for j in (0..n).chain(::std::iter::once(n)) {
count = (count + 3) * j;
}
count
}
fn addmul_loop_inclusive(n: u64) -> u64 {
let mut count = 0;
for j in 0..=n {
count = (count + 3) * j;
}
count
}
fn addmul_loop_fixed_inclusive(n: u64) -> u64 {
let mut count = 0;
for j in inclusive(0, n) {
count = (count + 3) * j;
}
count
}
fn criterion_addmul_loop(c: &mut Criterion) {
// c.bench_function("addmul loop exclusive 1,000,000", |b| b.iter(|| addmul_loop_exclusive(1_000_000)));
// c.bench_function("addmul loop chain 1,000,000", |b| b.iter(|| addmul_loop_chain(1_000_000)));
// c.bench_function("addmul loop inclusive 1,000,000", |b| b.iter(|| addmul_loop_inclusive(1_000_000)));
// c.bench_function("addmul loop fixed inclusive 1,000,000", |b| b.iter(|| addmul_loop_fixed_inclusive(1_000_000)));
c.bench_function("addmul loop exclusive bb 1,000,000", |b| b.iter(|| addmul_loop_exclusive(black_box(1_000_000))));
c.bench_function("addmul loop chain bb 1,000,000", |b| b.iter(|| addmul_loop_chain(black_box(1_000_000))));
c.bench_function("addmul loop inclusive bb 1,000,000", |b| b.iter(|| addmul_loop_inclusive(black_box(1_000_000))));
c.bench_function("addmul loop fixed inclusive bb 1,000,000", |b| b.iter(|| addmul_loop_fixed_inclusive(black_box(1_000_000))));
}
criterion_group!(addmul_loop, criterion_addmul_loop);
fn triples_exclusive(n: usize) -> u64 {
(1..).flat_map(|z| {
(1..(z + 1)).flat_map(move |x| {
(x..(z + 1)).filter_map(move |y| {
if x * x + y * y == z * z {
Some((x, y, z))
} else {
None
}
})
})
}).take(n).map(|(x, y, z)| x + y + z).sum()
}
fn triples_chain(n: usize) -> u64 {
(1..).flat_map(|z| {
(1..z).chain(::std::iter::once(z)).flat_map(move |x| {
(x..z).chain(::std::iter::once(z)).filter_map(move |y| {
if x * x + y * y == z * z {
Some((x, y, z))
} else {
None
}
})
})
}).take(n).map(|(x, y, z)| x + y + z).sum()
}
fn triples_inclusive(n: usize) -> u64 {
(1..).flat_map(|z| {
(1..=z).flat_map(move |x| {
(x..=z).filter_map(move |y| {
if x * x + y * y == z * z {
Some((x, y, z))
} else {
None
}
})
})
}).take(n).map(|(x, y, z)| x + y + z).sum()
}
fn triples_fixed_inclusive(n: usize) -> u64 {
(1..).flat_map(|z| {
inclusive(1, z).flat_map(move |x| {
inclusive(x, z).filter_map(move |y| {
if x * x + y * y == z * z {
Some((x, y, z))
} else {
None
}
})
})
}).take(n).map(|(x, y, z)| x + y + z).sum()
}
fn criterion_triples(c: &mut Criterion) {
// c.bench_function("triples exclusive 100", |b| b.iter(|| triples_exclusive(100)));
// c.bench_function("triples chain 100", |b| b.iter(|| triples_chain(100)));
// c.bench_function("triples inclusive 100", |b| b.iter(|| triples_inclusive(100)));
// c.bench_function("triples fixed inclusive 100", |b| b.iter(|| triples_fixed_inclusive(100)));
c.bench_function("triples exclusive bb 100", |b| b.iter(|| triples_exclusive(black_box(100))));
c.bench_function("triples chain bb 100", |b| b.iter(|| triples_chain(black_box(100))));
c.bench_function("triples inclusive bb 100", |b| b.iter(|| triples_inclusive(black_box(100))));
c.bench_function("triples fixed inclusive bb 100", |b| b.iter(|| triples_fixed_inclusive(black_box(100))));
}
criterion_group!(triples, criterion_triples);
criterion_main!(sum, triangle_foreach, triangle_loop, addmul_foreach, addmul_loop, triples);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment