Rust StringBuilder
| #![feature(test)] | |
| extern crate test; | |
| use test::Bencher; | |
| const LORUM: &'static str = r#"Lorem ipsum dolor sit amet, consectetur adipiscing | |
| elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
| quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
| dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur | |
| sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est | |
| laborum."#; | |
| #[bench] | |
| fn naive_string_building_short_loop(b: &mut Bencher) { | |
| let strings = vec!["how", "now", "brown", "cow"]; | |
| b.iter(|| { | |
| let mut s = String::from(strings[0]); | |
| for i in strings[1..].iter() { | |
| s = s + &i; | |
| } | |
| }) | |
| } | |
| #[bench] | |
| fn naive_string_building_lorum_loop(b: &mut Bencher) { | |
| let strings = vec![LORUM, LORUM, LORUM, LORUM]; | |
| b.iter(|| { | |
| let mut s = String::from(strings[0]); | |
| for i in strings[1..].iter() { | |
| s = s + &i; | |
| } | |
| }) | |
| } | |
| #[bench] | |
| fn naive_string_building_short_noloop(b: &mut Bencher) { | |
| b.iter(|| { | |
| let s = String::from("how"); | |
| s + "now" + "brown" + "cow"; | |
| }) | |
| } | |
| #[bench] | |
| fn naive_string_building_lorum_noloop(b: &mut Bencher) { | |
| b.iter(|| { | |
| let s = String::from(LORUM); | |
| s + LORUM + LORUM + LORUM; | |
| }) | |
| } | |
| #[bench] | |
| fn naive_string_building_short_loop_many(b: &mut Bencher) { | |
| let mut strings = vec!["how", "now", "brown", "cow"]; | |
| for _ in 0..10 { | |
| let mut new_strings = strings.clone(); | |
| strings.append(&mut new_strings); | |
| } | |
| b.iter(|| { | |
| let mut s = String::from(strings[0]); | |
| for i in strings[1..].iter() { | |
| s = s + &i; | |
| } | |
| }) | |
| } | |
| #[bench] | |
| fn naive_string_building_lorum_loop_many(b: &mut Bencher) { | |
| let mut strings = vec![LORUM, LORUM, LORUM, LORUM]; | |
| for _ in 0..10 { | |
| let mut new_strings = strings.clone(); | |
| strings.append(&mut new_strings); | |
| } | |
| b.iter(|| { | |
| let mut s = String::from(strings[0]); | |
| for i in strings[1..].iter() { | |
| s = s + &i; | |
| } | |
| }) | |
| } | |
| #[bench] | |
| fn format_string_building_short_noloop(b: &mut Bencher) { | |
| b.iter(|| { | |
| test::black_box(format!("{} {} {} {}", "how", "now", "brown", "cow")); | |
| }) | |
| } | |
| #[bench] | |
| fn format_string_building_lorum_noloop(b: &mut Bencher) { | |
| b.iter(|| { | |
| test::black_box(format!("{} {} {} {}", LORUM, LORUM, LORUM, LORUM)); | |
| }) | |
| } | |
| // Now with StringBuilder using a vector... | |
| struct StringBuilder<'a> { | |
| v: Vec<&'a str>, | |
| len: usize | |
| } | |
| impl<'a> StringBuilder<'a> { | |
| fn new() -> StringBuilder<'a> { | |
| StringBuilder{ v: Vec::new(), len: 0 } | |
| } | |
| fn append(&mut self, s: &'a str) { | |
| self.len += s.len(); | |
| self.v.push(s); | |
| } | |
| fn to_string(&self) -> String { | |
| let mut ret = String::with_capacity(self.len); | |
| for i in self.v.iter() { | |
| ret += i; | |
| } | |
| ret | |
| } | |
| } | |
| #[bench] | |
| fn stringbuilder_string_building_short_loop(b: &mut Bencher) { | |
| let strings = vec!["how", "now", "brown", "cow"]; | |
| b.iter(|| { | |
| let mut s = StringBuilder::new(); | |
| for i in strings[1..].iter() { | |
| s.append(*i); | |
| } | |
| test::black_box(s.to_string()); | |
| }) | |
| } | |
| #[bench] | |
| fn stringbuilder_string_building_short_loop_many(b: &mut Bencher) { | |
| let mut strings = vec!["how", "now", "brown", "cow"]; | |
| for _ in 0..10 { | |
| let mut new_strings = strings.clone(); | |
| strings.append(&mut new_strings); | |
| } | |
| b.iter(|| { | |
| let mut s = StringBuilder::new(); | |
| for i in strings[1..].iter() { | |
| s.append(*i); | |
| } | |
| test::black_box(s.to_string()); | |
| }) | |
| } | |
| #[bench] | |
| fn stringbuilder_string_building_lorum_loop(b: &mut Bencher) { | |
| let strings = vec![LORUM, LORUM, LORUM, LORUM]; | |
| b.iter(|| { | |
| let mut s = StringBuilder::new(); | |
| for i in strings[1..].iter() { | |
| s.append(*i); | |
| } | |
| test::black_box(s.to_string()); | |
| }) | |
| } | |
| #[bench] | |
| fn stringbuilder_string_building_lorum_loop_many(b: &mut Bencher) { | |
| let mut strings = vec![LORUM, LORUM, LORUM, LORUM]; | |
| for _ in 0..10 { | |
| let mut new_strings = strings.clone(); | |
| strings.append(&mut new_strings); | |
| } | |
| b.iter(|| { | |
| let mut s = StringBuilder::new(); | |
| for i in strings[1..].iter() { | |
| s.append(*i); | |
| } | |
| test::black_box(s.to_string()); | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment