|  | 
| 1 | 1 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; | 
| 2 | 2 | 
 | 
| 3 | 3 | fn get_bunch_of_strings() -> Vec<String> { | 
| 4 |  | -    let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap(); | 
| 5 |  | -    let mut files = files | 
| 6 |  | -        .into_iter() | 
| 7 |  | -        .map(|p| p.unwrap().canonicalize().unwrap()) | 
| 8 |  | -        .collect::<Vec<_>>(); | 
| 9 |  | -    files.sort(); | 
| 10 |  | -    let stirngs = files | 
| 11 |  | -        .iter() | 
| 12 |  | -        .map(|p| std::fs::read_to_string(p).unwrap()) | 
| 13 |  | -        .collect::<Vec<_>>(); | 
|  | 4 | +  let files = glob::glob("fixtures/threejs_src/**/*.js").unwrap(); | 
|  | 5 | +  let mut files = files.into_iter().map(|p| p.unwrap().canonicalize().unwrap()).collect::<Vec<_>>(); | 
|  | 6 | +  files.sort(); | 
|  | 7 | +  let stirngs = files.iter().map(|p| std::fs::read_to_string(p).unwrap()).collect::<Vec<_>>(); | 
| 14 | 8 | 
 | 
| 15 |  | -    let mut ret = vec![]; | 
| 16 |  | -    for _ in 0..10 { | 
| 17 |  | -        ret.extend(stirngs.clone()); | 
| 18 |  | -    } | 
| 19 |  | -    ret | 
|  | 9 | +  let mut ret = vec![]; | 
|  | 10 | +  for _ in 0..10 { | 
|  | 11 | +    ret.extend(stirngs.clone()); | 
|  | 12 | +  } | 
|  | 13 | +  ret | 
| 20 | 14 | } | 
| 21 | 15 | 
 | 
| 22 | 16 | fn criterion_benchmark(c: &mut Criterion) { | 
| 23 |  | -    let bunch_of_strings = get_bunch_of_strings(); | 
| 24 |  | -   | 
| 25 |  | -    let mut joiner = string_wizard::Joiner::new(); | 
| 26 |  | -    bunch_of_strings.clone().into_iter().for_each(|s| { | 
| 27 |  | -        joiner.append_raw(s); | 
| 28 |  | -    }); | 
| 29 |  | -    c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join()))); | 
| 30 |  | -    c.bench_function("Vec#concat", |b| { | 
| 31 |  | -        b.iter(|| black_box(bunch_of_strings.concat())) | 
| 32 |  | -    }); | 
| 33 |  | -    c.bench_function("manual_push", |b| { | 
| 34 |  | -        b.iter(|| { | 
| 35 |  | -            let mut output = String::new(); | 
| 36 |  | -            bunch_of_strings.iter().for_each(|s| { | 
| 37 |  | -                output.push_str(s); | 
| 38 |  | -            }); | 
| 39 |  | -            black_box(output) | 
| 40 |  | -        }) | 
| 41 |  | -    }); | 
| 42 |  | -    c.bench_function("manual_push_with_cap", |b| { | 
| 43 |  | -        b.iter(|| { | 
| 44 |  | -            let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum(); | 
| 45 |  | -            let mut output = String::with_capacity(cap); | 
| 46 |  | -            bunch_of_strings.iter().for_each(|s| { | 
| 47 |  | -                output.push_str(s); | 
| 48 |  | -            }); | 
| 49 |  | -            black_box(output) | 
| 50 |  | -        }) | 
| 51 |  | -    }); | 
|  | 17 | +  let bunch_of_strings = get_bunch_of_strings(); | 
|  | 18 | + | 
|  | 19 | +  let mut joiner = string_wizard::Joiner::new(); | 
|  | 20 | +  bunch_of_strings.clone().into_iter().for_each(|s| { | 
|  | 21 | +    joiner.append_raw(s); | 
|  | 22 | +  }); | 
|  | 23 | +  c.bench_function("Joiner#join", |b| b.iter(|| black_box(joiner.join()))); | 
|  | 24 | +  c.bench_function("Vec#concat", |b| b.iter(|| black_box(bunch_of_strings.concat()))); | 
|  | 25 | +  c.bench_function("manual_push", |b| { | 
|  | 26 | +    b.iter(|| { | 
|  | 27 | +      let mut output = String::new(); | 
|  | 28 | +      bunch_of_strings.iter().for_each(|s| { | 
|  | 29 | +        output.push_str(s); | 
|  | 30 | +      }); | 
|  | 31 | +      black_box(output) | 
|  | 32 | +    }) | 
|  | 33 | +  }); | 
|  | 34 | +  c.bench_function("manual_push_with_cap", |b| { | 
|  | 35 | +    b.iter(|| { | 
|  | 36 | +      let cap: usize = bunch_of_strings.iter().map(|s| s.len()).sum(); | 
|  | 37 | +      let mut output = String::with_capacity(cap); | 
|  | 38 | +      bunch_of_strings.iter().for_each(|s| { | 
|  | 39 | +        output.push_str(s); | 
|  | 40 | +      }); | 
|  | 41 | +      black_box(output) | 
|  | 42 | +    }) | 
|  | 43 | +  }); | 
| 52 | 44 | } | 
| 53 | 45 | 
 | 
| 54 | 46 | criterion_group!(benches, criterion_benchmark); | 
|  | 
0 commit comments