| 1 |
|
| 2 |
extern crate arrayvec; |
| 3 |
#[macro_use] extern crate bencher; |
| 4 |
|
| 5 |
use arrayvec::ArrayString; |
| 6 |
|
| 7 |
use bencher::Bencher; |
| 8 |
|
| 9 |
fn try_push_c(b: &mut Bencher) { |
| 10 |
let mut v = ArrayString::<512>::new(); |
| 11 |
b.iter(|| { |
| 12 |
v.clear(); |
| 13 |
while v.try_push('c').is_ok() { |
| 14 |
} |
| 15 |
v.len() |
| 16 |
}); |
| 17 |
b.bytes = v.capacity() as u64; |
| 18 |
} |
| 19 |
|
| 20 |
fn try_push_alpha(b: &mut Bencher) { |
| 21 |
let mut v = ArrayString::<512>::new(); |
| 22 |
b.iter(|| { |
| 23 |
v.clear(); |
| 24 |
while v.try_push('α').is_ok() { |
| 25 |
} |
| 26 |
v.len() |
| 27 |
}); |
| 28 |
b.bytes = v.capacity() as u64; |
| 29 |
} |
| 30 |
|
| 31 |
// Yes, pushing a string char-by-char is slow. Use .push_str. |
| 32 |
fn try_push_string(b: &mut Bencher) { |
| 33 |
let mut v = ArrayString::<512>::new(); |
| 34 |
let input = "abcαβγ“”"; |
| 35 |
b.iter(|| { |
| 36 |
v.clear(); |
| 37 |
for ch in input.chars().cycle() { |
| 38 |
if !v.try_push(ch).is_ok() { |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
v.len() |
| 43 |
}); |
| 44 |
b.bytes = v.capacity() as u64; |
| 45 |
} |
| 46 |
|
| 47 |
fn push_c(b: &mut Bencher) { |
| 48 |
let mut v = ArrayString::<512>::new(); |
| 49 |
b.iter(|| { |
| 50 |
v.clear(); |
| 51 |
while !v.is_full() { |
| 52 |
v.push('c'); |
| 53 |
} |
| 54 |
v.len() |
| 55 |
}); |
| 56 |
b.bytes = v.capacity() as u64; |
| 57 |
} |
| 58 |
|
| 59 |
fn push_alpha(b: &mut Bencher) { |
| 60 |
let mut v = ArrayString::<512>::new(); |
| 61 |
b.iter(|| { |
| 62 |
v.clear(); |
| 63 |
while !v.is_full() { |
| 64 |
v.push('α'); |
| 65 |
} |
| 66 |
v.len() |
| 67 |
}); |
| 68 |
b.bytes = v.capacity() as u64; |
| 69 |
} |
| 70 |
|
| 71 |
fn push_string(b: &mut Bencher) { |
| 72 |
let mut v = ArrayString::<512>::new(); |
| 73 |
let input = "abcαβγ“”"; |
| 74 |
b.iter(|| { |
| 75 |
v.clear(); |
| 76 |
for ch in input.chars().cycle() { |
| 77 |
if !v.is_full() { |
| 78 |
v.push(ch); |
| 79 |
} else { |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
v.len() |
| 84 |
}); |
| 85 |
b.bytes = v.capacity() as u64; |
| 86 |
} |
| 87 |
|
| 88 |
benchmark_group!(benches, try_push_c, try_push_alpha, try_push_string, push_c, |
| 89 |
push_alpha, push_string); |
| 90 |
benchmark_main!(benches); |