2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-21 17:57:14 -05:00
|
|
|
// ignore-lexer-test FIXME #15679
|
2013-05-20 19:07:24 -05:00
|
|
|
// Microbenchmarks for various functions in std and extra
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2014-12-08 07:35:34 -06:00
|
|
|
#![feature(unboxed_closures)]
|
2013-10-02 22:00:54 -05:00
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io::File;
|
2015-01-02 01:53:35 -06:00
|
|
|
use std::iter::repeat;
|
2014-01-31 14:35:36 -06:00
|
|
|
use std::mem::swap;
|
2015-02-13 14:08:05 -06:00
|
|
|
use std::env;
|
2014-11-10 14:27:56 -06:00
|
|
|
use std::rand::Rng;
|
|
|
|
use std::rand;
|
2013-07-11 13:45:45 -05:00
|
|
|
use std::str;
|
2014-11-10 14:27:56 -06:00
|
|
|
use std::time::Duration;
|
2014-03-05 16:02:44 -06:00
|
|
|
use std::vec;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2015-02-16 08:04:02 -06:00
|
|
|
let argv: Vec<String> = env::args().collect();
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2015-01-02 16:44:21 -06:00
|
|
|
macro_rules! bench {
|
2014-02-03 18:44:47 -06:00
|
|
|
($id:ident) =>
|
2015-02-01 20:53:25 -06:00
|
|
|
(maybe_run_test(&argv,
|
2014-02-03 18:44:47 -06:00
|
|
|
stringify!($id).to_string(),
|
2015-01-02 16:44:21 -06:00
|
|
|
$id))
|
|
|
|
}
|
2014-02-03 18:44:47 -06:00
|
|
|
|
|
|
|
bench!(shift_push);
|
|
|
|
bench!(read_line);
|
|
|
|
bench!(vec_plus);
|
|
|
|
bench!(vec_append);
|
|
|
|
bench!(vec_push_all);
|
|
|
|
bench!(is_utf8_ascii);
|
|
|
|
bench!(is_utf8_multibyte);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 07:35:34 -06:00
|
|
|
fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() {
|
2012-06-28 00:30:08 -05:00
|
|
|
let mut run_test = false;
|
|
|
|
|
2015-02-13 14:08:05 -06:00
|
|
|
if env::var_os("RUST_BENCH").is_some() {
|
2013-05-06 21:29:04 -05:00
|
|
|
run_test = true
|
|
|
|
} else if argv.len() > 0 {
|
2014-05-25 05:17:19 -05:00
|
|
|
run_test = argv.iter().any(|x| x == &"all".to_string()) || argv.iter().any(|x| x == &name)
|
2012-06-28 00:30:08 -05:00
|
|
|
}
|
|
|
|
|
2013-05-06 21:29:04 -05:00
|
|
|
if !run_test {
|
|
|
|
return
|
|
|
|
}
|
2012-06-28 00:30:08 -05:00
|
|
|
|
2014-11-10 14:27:56 -06:00
|
|
|
let dur = Duration::span(test);
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2014-11-10 14:27:56 -06:00
|
|
|
println!("{}:\t\t{} ms", name, dur.num_milliseconds());
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_push() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut v1 = repeat(1).take(30000).collect::<Vec<_>>();
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v2 = Vec::new();
|
2012-06-26 13:13:02 -05:00
|
|
|
|
|
|
|
while v1.len() > 0 {
|
2014-12-30 12:51:18 -06:00
|
|
|
v2.push(v1.remove(0));
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line() {
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io::BufferedReader;
|
2013-10-06 18:08:56 -05:00
|
|
|
|
2013-12-03 21:15:12 -06:00
|
|
|
let mut path = Path::new(env!("CFG_SRC_DIR"));
|
2013-10-05 21:49:32 -05:00
|
|
|
path.push("src/test/bench/shootout-k-nucleotide.data");
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0u..3 {
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut reader = BufferedReader::new(File::open(&path).unwrap());
|
2013-12-08 01:12:07 -06:00
|
|
|
for _line in reader.lines() {
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-06-27 18:08:22 -05:00
|
|
|
fn vec_plus() {
|
2014-12-27 18:20:47 -06:00
|
|
|
let mut r = rand::thread_rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2015-01-02 01:53:35 -06:00
|
|
|
let rv = repeat(i).take(r.gen_range(0u, i + 1)).collect::<Vec<_>>();
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2014-10-15 01:05:01 -05:00
|
|
|
v.extend(rv.into_iter());
|
2013-06-11 21:13:42 -05:00
|
|
|
} else {
|
2014-10-15 01:05:01 -05:00
|
|
|
let mut rv = rv.clone();
|
2015-02-01 20:53:25 -06:00
|
|
|
rv.push_all(&v);
|
2014-10-15 01:05:01 -05:00
|
|
|
v = rv;
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
2012-06-27 18:08:22 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_append() {
|
2014-12-27 18:20:47 -06:00
|
|
|
let mut r = rand::thread_rng();
|
2012-06-27 18:08:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2015-01-02 01:53:35 -06:00
|
|
|
let rv = repeat(i).take(r.gen_range(0u, i + 1)).collect::<Vec<_>>();
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2014-10-15 01:05:01 -05:00
|
|
|
let mut t = v.clone();
|
2015-02-01 20:53:25 -06:00
|
|
|
t.push_all(&rv);
|
2014-10-15 01:05:01 -05:00
|
|
|
v = t;
|
2012-06-27 18:08:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-10-15 01:05:01 -05:00
|
|
|
let mut t = rv.clone();
|
2015-02-01 20:53:25 -06:00
|
|
|
t.push_all(&v);
|
2014-10-15 01:05:01 -05:00
|
|
|
v = t;
|
2012-06-27 18:08:22 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_push_all() {
|
2014-12-27 18:20:47 -06:00
|
|
|
let mut r = rand::thread_rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0u..1500 {
|
2015-01-02 01:53:35 -06:00
|
|
|
let mut rv = repeat(i).take(r.gen_range(0u, i + 1)).collect::<Vec<_>>();
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2015-02-01 20:53:25 -06:00
|
|
|
v.push_all(&rv);
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-01-31 14:35:36 -06:00
|
|
|
swap(&mut v, &mut rv);
|
2015-02-01 20:53:25 -06:00
|
|
|
v.push_all(&rv);
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 13:45:45 -05:00
|
|
|
|
|
|
|
fn is_utf8_ascii() {
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0u..20000 {
|
2013-07-11 13:45:45 -05:00
|
|
|
v.push('b' as u8);
|
2015-02-01 20:53:25 -06:00
|
|
|
if str::from_utf8(&v).is_err() {
|
2015-01-02 01:53:35 -06:00
|
|
|
panic!("from_utf8 panicked");
|
2013-07-11 13:45:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_utf8_multibyte() {
|
|
|
|
let s = "b¢€𤭢";
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0u..5000 {
|
2013-07-11 13:45:45 -05:00
|
|
|
v.push_all(s.as_bytes());
|
2015-02-01 20:53:25 -06:00
|
|
|
if str::from_utf8(&v).is_err() {
|
2015-01-02 01:53:35 -06:00
|
|
|
panic!("from_utf8 panicked");
|
2013-07-11 13:45:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|