2012-12-10 17:32:48 -08: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 15:57:14 -07:00
|
|
|
// ignore-lexer-test FIXME #15679
|
2013-05-20 17:07:24 -07:00
|
|
|
// Microbenchmarks for various functions in std and extra
|
2012-06-26 11:13:02 -07:00
|
|
|
|
2014-04-14 21:00:31 +05:30
|
|
|
#![feature(macro_rules)]
|
2014-12-08 08:35:34 -05:00
|
|
|
#![feature(unboxed_closures)]
|
2013-10-02 20:00:54 -07:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::io::File;
|
2014-02-01 04:35:36 +08:00
|
|
|
use std::mem::swap;
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::os;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::rand::Rng;
|
|
|
|
use std::rand;
|
2013-07-11 14:45:45 -04:00
|
|
|
use std::str;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::time::Duration;
|
2014-03-05 14:02:44 -08:00
|
|
|
use std::vec;
|
2012-06-26 11:13:02 -07:00
|
|
|
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
2014-06-26 08:15:14 +02:00
|
|
|
let argv = os::args();
|
2013-07-17 15:31:20 -04:00
|
|
|
let _tests = argv.slice(1, argv.len());
|
2012-06-26 14:04:15 -07:00
|
|
|
|
2014-02-03 16:44:47 -08:00
|
|
|
macro_rules! bench (
|
|
|
|
($id:ident) =>
|
|
|
|
(maybe_run_test(argv.as_slice(),
|
|
|
|
stringify!($id).to_string(),
|
|
|
|
$id)))
|
|
|
|
|
|
|
|
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 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 08:35:34 -05:00
|
|
|
fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() {
|
2012-06-27 22:30:08 -07:00
|
|
|
let mut run_test = false;
|
|
|
|
|
2013-07-17 15:31:20 -04:00
|
|
|
if os::getenv("RUST_BENCH").is_some() {
|
2013-05-06 19:29:04 -07:00
|
|
|
run_test = true
|
|
|
|
} else if argv.len() > 0 {
|
2014-05-25 03:17:19 -07:00
|
|
|
run_test = argv.iter().any(|x| x == &"all".to_string()) || argv.iter().any(|x| x == &name)
|
2012-06-27 22:30:08 -07:00
|
|
|
}
|
|
|
|
|
2013-05-06 19:29:04 -07:00
|
|
|
if !run_test {
|
|
|
|
return
|
|
|
|
}
|
2012-06-27 22:30:08 -07:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
let dur = Duration::span(test);
|
2012-06-26 11:13:02 -07:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
println!("{}:\t\t{} ms", name, dur.num_milliseconds());
|
2012-06-26 11:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_push() {
|
2014-04-21 17:58:52 -04:00
|
|
|
let mut v1 = Vec::from_elem(30000, 1i);
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v2 = Vec::new();
|
2012-06-26 11:13:02 -07:00
|
|
|
|
|
|
|
while v1.len() > 0 {
|
2014-10-14 23:05:01 -07:00
|
|
|
v2.push(v1.remove(0).unwrap());
|
2012-06-26 11:13:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line() {
|
2014-01-15 13:25:09 -08:00
|
|
|
use std::io::BufferedReader;
|
2013-10-06 16:08:56 -07:00
|
|
|
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut path = Path::new(env!("CFG_SRC_DIR"));
|
2013-10-05 19:49:32 -07:00
|
|
|
path.push("src/test/bench/shootout-k-nucleotide.data");
|
2012-06-26 11:13:02 -07:00
|
|
|
|
2014-04-21 17:58:52 -04:00
|
|
|
for _ in range(0u, 3) {
|
2013-10-29 23:31:07 -07:00
|
|
|
let mut reader = BufferedReader::new(File::open(&path).unwrap());
|
2013-12-08 16:12:07 +09:00
|
|
|
for _line in reader.lines() {
|
2012-06-26 11:13:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 14:04:15 -07:00
|
|
|
|
2012-06-27 16:08:22 -07:00
|
|
|
fn vec_plus() {
|
2014-03-02 12:59:35 +11:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 11:32:22 -07:00
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 16:08:22 -07:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2014-03-05 15:28:08 -08:00
|
|
|
let rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 22:29:19 +10:00
|
|
|
if r.gen() {
|
2014-10-14 23:05:01 -07:00
|
|
|
v.extend(rv.into_iter());
|
2013-06-11 19:13:42 -07:00
|
|
|
} else {
|
2014-10-14 23:05:01 -07:00
|
|
|
let mut rv = rv.clone();
|
|
|
|
rv.push_all(v.as_slice());
|
|
|
|
v = rv;
|
2012-06-27 11:32:22 -07:00
|
|
|
}
|
2012-06-27 16:08:22 -07:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_append() {
|
2014-03-02 12:59:35 +11:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 16:08:22 -07:00
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 16:08:22 -07:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2014-03-05 15:28:08 -08:00
|
|
|
let rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 22:29:19 +10:00
|
|
|
if r.gen() {
|
2014-10-14 23:05:01 -07:00
|
|
|
let mut t = v.clone();
|
|
|
|
t.push_all(rv.as_slice());
|
|
|
|
v = t;
|
2012-06-27 16:08:22 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-10-14 23:05:01 -07:00
|
|
|
let mut t = rv.clone();
|
|
|
|
t.push_all(v.as_slice());
|
|
|
|
v = t;
|
2012-06-27 16:08:22 -07:00
|
|
|
}
|
|
|
|
i += 1;
|
2012-06-27 11:32:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_push_all() {
|
2014-03-02 12:59:35 +11:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 11:32:22 -07:00
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v = Vec::new();
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, 1500) {
|
2014-03-05 15:28:08 -08:00
|
|
|
let mut rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 22:29:19 +10:00
|
|
|
if r.gen() {
|
2014-03-05 15:28:08 -08:00
|
|
|
v.push_all(rv.as_slice());
|
2012-06-27 11:32:22 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-02-01 04:35:36 +08:00
|
|
|
swap(&mut v, &mut rv);
|
2014-03-05 15:28:08 -08:00
|
|
|
v.push_all(rv.as_slice());
|
2012-06-27 11:32:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 14:45:45 -04:00
|
|
|
|
|
|
|
fn is_utf8_ascii() {
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2013-08-03 12:45:23 -04:00
|
|
|
for _ in range(0u, 20000) {
|
2013-07-11 14:45:45 -04:00
|
|
|
v.push('b' as u8);
|
2014-03-05 15:28:08 -08:00
|
|
|
if !str::is_utf8(v.as_slice()) {
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("is_utf8 panicked");
|
2013-07-11 14:45:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_utf8_multibyte() {
|
|
|
|
let s = "b¢€𤭢";
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2013-08-03 12:45:23 -04:00
|
|
|
for _ in range(0u, 5000) {
|
2013-07-11 14:45:45 -04:00
|
|
|
v.push_all(s.as_bytes());
|
2014-03-05 15:28:08 -08:00
|
|
|
if !str::is_utf8(v.as_slice()) {
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("is_utf8 panicked");
|
2013-07-11 14:45:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|