Skip to content

Commit

Permalink
verdict from the bench: taking stock is free, updating is ruinous
Browse files Browse the repository at this point in the history
The word upstream is that the unstable builtin benchmark tests are
likely to be deprecated (rust-lang/rust#29553), but I didn't feel like
learning Criterion just for this.

test
inference::triangle::tests::concerning_the_expense_of_computing_entropy
... bench:           0 ns/iter (+/- 0)
test inference::triangle::tests::concerning_the_expense_of_updating
... bench:   1,485,967 ns/iter (+/- 81,055)

I'm not sure how to interpret the zero for computing the entropy; I
assume some sort of clever LLVM optimization has sloughed off the work
to compile time.

But yeah, that 1.5 million nanoseconds to do an update is way too explosive
for a hotspot like `value_of_information`. We're going to need some new
techniques.
  • Loading branch information
zackmdavis committed Feb 7, 2016
1 parent 5ee5941 commit 877512d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/inference/triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ pub fn complexity_prior(basic_hypotheses: Vec<BasicHypothesis>)

#[cfg(test)]
mod tests {
use test::Bencher;

use super::*;
use play::triangle::our_basic_hypotheses;
use triangles::{Color, Size, Stack, Study, Triangle};
use inference::triangle::hypotheses::{BasicHypothesis, JoinedHypothesis};
use inference::triangle::hypotheses::color_count_boundedness::ColorCountBoundednessHypothesis;
Expand Down Expand Up @@ -234,4 +237,20 @@ mod tests {
Color::Red, 1)))));
}

#[bench]
fn concerning_the_expense_of_updating(bencher: &mut Bencher) {
let distribution = complexity_prior(our_basic_hypotheses());
bencher.iter(|| {
distribution.updated(&Study::sample(), true);
});
}

#[bench]
fn concerning_the_expense_of_computing_entropy(bencher: &mut Bencher) {
let distribution = complexity_prior(our_basic_hypotheses());
bencher.iter(|| {
distribution.entropy();

This comment has been minimized.

Copy link
@CAD97

CAD97 Aug 13, 2017

Remove the ;. It takes 0 ns to run this because the optimizer realizes that this is a pure fn and you don't use the result, so it just deletes it. Returning the result will cause the optimizer to not do so.

(Yes, I checked master and this is still there.)

https://doc.rust-lang.org/1.12.1/book/benchmark-tests.html#gotcha-optimizations

This comment has been minimized.

Copy link
@zackmdavis

zackmdavis Aug 13, 2017

Author Owner

Wow. Thanks! On nightly (f774bced5 2017-08-12), the semicoloned version isn't zero anymore, but there's still a huge difference, which is puzzling.

(I'm flattered that you read my code! Were you specifically searching GitHub for confused benchtest users, or ...?)

This comment has been minimized.

Copy link
@CAD97

CAD97 Aug 13, 2017

Happened upon it while googling rust benchmark trying to find reference material to share elsewhere.

});
}

}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![feature(iter_arith)]
#![feature(iter_arith, test)]

extern crate argparse;
extern crate ansi_term;
extern crate itertools;
extern crate rand;
extern crate test;

#[macro_use] mod display;
#[macro_use] mod triangles;
Expand Down
2 changes: 1 addition & 1 deletion src/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod number;
mod triangle;
pub mod triangle;


use argparse::{ArgumentParser, Store, StoreTrue};
Expand Down
21 changes: 13 additions & 8 deletions src/play/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ use inference::triangle::hypotheses::color_count_boundedness::ColorCountBoundedn
use inference::triangle::hypotheses::size_count_boundedness::SizeCountBoundednessHypothesis;


pub fn play() {
wrapln!("Welcome to Mezzanine v. {}! Privately think of a criterion. \
This program will attempt to efficiently infer the nature of \
the criterion by asking you whether specific studies do or do \
not have the property of satisfying the criterion.",
env!("CARGO_PKG_VERSION"));

pub fn our_basic_hypotheses() -> Vec<BasicHypothesis> {
let mut hypotheses = Vec::new();
for &color in Color::iter() {
for lower in 1..4 {
Expand Down Expand Up @@ -45,8 +39,19 @@ pub fn play() {
size, upper)));
}
}
hypotheses
}


pub fn play() {
wrapln!("Welcome to Mezzanine v. {}! Privately think of a criterion. \
This program will attempt to efficiently infer the nature of \
the criterion by asking you whether specific studies do or do \
not have the property of satisfying the criterion.",
env!("CARGO_PKG_VERSION"));

let mut beliefs = complexity_prior(hypotheses);
let basic_hypotheses = our_basic_hypotheses();
let mut beliefs = complexity_prior(basic_hypotheses);
println!("Size of hypothesis space: {}", beliefs.len());

loop {
Expand Down

0 comments on commit 877512d

Please sign in to comment.