]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | // Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
970d7e83 | 11 | // Microbenchmarks for various functions in std and extra |
223e47cc | 12 | |
62682a34 | 13 | #![feature(rand, vec_push_all, duration, duration_span)] |
223e47cc | 14 | |
1a4d82fc JJ |
15 | use std::iter::repeat; |
16 | use std::mem::swap; | |
85aaf69f | 17 | use std::env; |
9346a6ac | 18 | use std::__rand::{thread_rng, Rng}; |
1a4d82fc JJ |
19 | use std::str; |
20 | use std::time::Duration; | |
223e47cc | 21 | |
223e47cc | 22 | fn main() { |
85aaf69f | 23 | let argv: Vec<String> = env::args().collect(); |
1a4d82fc JJ |
24 | |
25 | macro_rules! bench { | |
26 | ($id:ident) => | |
85aaf69f | 27 | (maybe_run_test(&argv, |
1a4d82fc JJ |
28 | stringify!($id).to_string(), |
29 | $id)) | |
30 | } | |
223e47cc LB |
31 | |
32 | bench!(shift_push); | |
223e47cc LB |
33 | bench!(vec_plus); |
34 | bench!(vec_append); | |
35 | bench!(vec_push_all); | |
1a4d82fc JJ |
36 | bench!(is_utf8_ascii); |
37 | bench!(is_utf8_multibyte); | |
223e47cc LB |
38 | } |
39 | ||
1a4d82fc | 40 | fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() { |
223e47cc LB |
41 | let mut run_test = false; |
42 | ||
85aaf69f | 43 | if env::var_os("RUST_BENCH").is_some() { |
970d7e83 | 44 | run_test = true |
9346a6ac | 45 | } else if !argv.is_empty() { |
1a4d82fc | 46 | run_test = argv.iter().any(|x| x == &"all".to_string()) || argv.iter().any(|x| x == &name) |
223e47cc LB |
47 | } |
48 | ||
970d7e83 LB |
49 | if !run_test { |
50 | return | |
51 | } | |
223e47cc | 52 | |
1a4d82fc | 53 | let dur = Duration::span(test); |
223e47cc | 54 | |
d9579d0f | 55 | println!("{}:\t\t{}", name, dur); |
223e47cc LB |
56 | } |
57 | ||
58 | fn shift_push() { | |
85aaf69f | 59 | let mut v1 = repeat(1).take(30000).collect::<Vec<_>>(); |
1a4d82fc | 60 | let mut v2 = Vec::new(); |
223e47cc | 61 | |
9346a6ac | 62 | while !v1.is_empty() { |
1a4d82fc | 63 | v2.push(v1.remove(0)); |
223e47cc LB |
64 | } |
65 | } | |
66 | ||
223e47cc | 67 | fn vec_plus() { |
9346a6ac | 68 | let mut r = thread_rng(); |
223e47cc | 69 | |
1a4d82fc | 70 | let mut v = Vec::new(); |
223e47cc LB |
71 | let mut i = 0; |
72 | while i < 1500 { | |
85aaf69f | 73 | let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>(); |
970d7e83 | 74 | if r.gen() { |
62682a34 | 75 | v.extend(rv); |
970d7e83 | 76 | } else { |
1a4d82fc | 77 | let mut rv = rv.clone(); |
85aaf69f | 78 | rv.push_all(&v); |
1a4d82fc | 79 | v = rv; |
223e47cc LB |
80 | } |
81 | i += 1; | |
82 | } | |
83 | } | |
84 | ||
85 | fn vec_append() { | |
9346a6ac | 86 | let mut r = thread_rng(); |
223e47cc | 87 | |
1a4d82fc | 88 | let mut v = Vec::new(); |
223e47cc LB |
89 | let mut i = 0; |
90 | while i < 1500 { | |
85aaf69f | 91 | let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>(); |
970d7e83 | 92 | if r.gen() { |
1a4d82fc | 93 | let mut t = v.clone(); |
85aaf69f | 94 | t.push_all(&rv); |
1a4d82fc | 95 | v = t; |
223e47cc LB |
96 | } |
97 | else { | |
1a4d82fc | 98 | let mut t = rv.clone(); |
85aaf69f | 99 | t.push_all(&v); |
1a4d82fc | 100 | v = t; |
223e47cc LB |
101 | } |
102 | i += 1; | |
103 | } | |
104 | } | |
105 | ||
106 | fn vec_push_all() { | |
9346a6ac | 107 | let mut r = thread_rng(); |
223e47cc | 108 | |
1a4d82fc | 109 | let mut v = Vec::new(); |
85aaf69f SL |
110 | for i in 0..1500 { |
111 | let mut rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>(); | |
970d7e83 | 112 | if r.gen() { |
85aaf69f | 113 | v.push_all(&rv); |
223e47cc LB |
114 | } |
115 | else { | |
1a4d82fc | 116 | swap(&mut v, &mut rv); |
85aaf69f | 117 | v.push_all(&rv); |
1a4d82fc JJ |
118 | } |
119 | } | |
120 | } | |
121 | ||
122 | fn is_utf8_ascii() { | |
123 | let mut v : Vec<u8> = Vec::new(); | |
85aaf69f | 124 | for _ in 0..20000 { |
1a4d82fc | 125 | v.push('b' as u8); |
85aaf69f | 126 | if str::from_utf8(&v).is_err() { |
1a4d82fc JJ |
127 | panic!("from_utf8 panicked"); |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | fn is_utf8_multibyte() { | |
133 | let s = "b¢€𤭢"; | |
134 | let mut v : Vec<u8> = Vec::new(); | |
85aaf69f | 135 | for _ in 0..5000 { |
1a4d82fc | 136 | v.push_all(s.as_bytes()); |
85aaf69f | 137 | if str::from_utf8(&v).is_err() { |
1a4d82fc | 138 | panic!("from_utf8 panicked"); |
223e47cc LB |
139 | } |
140 | } | |
141 | } |