Don't print to stdout on `cargo bench`

This commit is contained in:
Alex Crichton 2015-05-26 07:21:12 -07:00
parent a6a399d52e
commit 82645a31df
2 changed files with 16 additions and 19 deletions

View File

@ -16,3 +16,7 @@ rational, and complex types.
[dependencies] [dependencies]
rustc-serialize = "0.3.13" rustc-serialize = "0.3.13"
rand = "0.3.8" rand = "0.3.8"
[[bench]]
name = "shootout-pidigits"
harness = false

View File

@ -38,16 +38,12 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE. // OF THE POSSIBILITY OF SUCH DAMAGE.
#![feature(test)]
extern crate num; extern crate num;
extern crate test;
use std::str::FromStr; use std::str::FromStr;
use std::io;
use num::traits::{FromPrimitive, ToPrimitive}; use num::traits::{FromPrimitive, ToPrimitive};
use test::Bencher;
use num::{BigInt, Integer, One, Zero}; use num::{BigInt, Integer, One, Zero};
struct Context { struct Context {
@ -93,7 +89,7 @@ impl Context {
} }
} }
fn pidigits(n: isize) { fn pidigits(n: isize, out: &mut io::Write) -> io::Result<()> {
let mut k = 0; let mut k = 0;
let mut context = Context::new(); let mut context = Context::new();
@ -106,33 +102,30 @@ fn pidigits(n: isize) {
if d != -1 {break;} if d != -1 {break;}
} }
print!("{}", d); try!(write!(out, "{}", d));
if i % 10 == 0 {print!("\t:{}\n", i);} if i % 10 == 0 { try!(write!(out, "\t:{}\n", i)); }
context.eliminate_digit(d); context.eliminate_digit(d);
} }
let m = n % 10; let m = n % 10;
if m != 0 { if m != 0 {
for _ in (m..10) { print!(" "); } for _ in (m..10) { try!(write!(out, " ")); }
print!("\t:{}\n", n); try!(write!(out, "\t:{}\n", n));
} }
Ok(())
} }
static DEFAULT_DIGITS: isize = 512; const DEFAULT_DIGITS: isize = 512;
#[bench]
fn use_bencher(b: &mut Bencher) {
b.iter(|| pidigits(DEFAULT_DIGITS))
}
#[allow(dead_code)]
fn main() { fn main() {
let args = std::env::args().collect::<Vec<_>>(); let args = std::env::args().collect::<Vec<_>>();
let n = if args.len() < 2 { let n = if args.len() < 2 {
DEFAULT_DIGITS DEFAULT_DIGITS
} else if args[1] == "--bench" {
return pidigits(DEFAULT_DIGITS, &mut std::io::sink()).unwrap()
} else { } else {
FromStr::from_str(&args[1]).unwrap() FromStr::from_str(&args[1]).unwrap()
}; };
pidigits(n); pidigits(n, &mut std::io::stdout()).unwrap();
} }