word_bounds.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #[macro_use]
  2. extern crate bencher;
  3. extern crate unicode_segmentation;
  4. use bencher::Bencher;
  5. use std::fs;
  6. use unicode_segmentation::UnicodeSegmentation;
  7. fn word_bounds(bench: &mut Bencher, path: &str) {
  8. let text = fs::read_to_string(path).unwrap();
  9. bench.iter(|| {
  10. for w in text.split_word_bounds() {
  11. bencher::black_box(w);
  12. }
  13. });
  14. bench.bytes = text.len() as u64;
  15. }
  16. fn word_bounds_arabic(bench: &mut Bencher) {
  17. word_bounds(bench, "benches/texts/arabic.txt");
  18. }
  19. fn word_bounds_english(bench: &mut Bencher) {
  20. word_bounds(bench, "benches/texts/english.txt");
  21. }
  22. fn word_bounds_hindi(bench: &mut Bencher) {
  23. word_bounds(bench, "benches/texts/hindi.txt");
  24. }
  25. fn word_bounds_japanese(bench: &mut Bencher) {
  26. word_bounds(bench, "benches/texts/japanese.txt");
  27. }
  28. fn word_bounds_korean(bench: &mut Bencher) {
  29. word_bounds(bench, "benches/texts/korean.txt");
  30. }
  31. fn word_bounds_mandarin(bench: &mut Bencher) {
  32. word_bounds(bench, "benches/texts/mandarin.txt");
  33. }
  34. fn word_bounds_russian(bench: &mut Bencher) {
  35. word_bounds(bench, "benches/texts/russian.txt");
  36. }
  37. fn word_bounds_source_code(bench: &mut Bencher) {
  38. word_bounds(bench, "benches/texts/source_code.txt");
  39. }
  40. benchmark_group!(
  41. benches,
  42. word_bounds_arabic,
  43. word_bounds_english,
  44. word_bounds_hindi,
  45. word_bounds_japanese,
  46. word_bounds_korean,
  47. word_bounds_mandarin,
  48. word_bounds_russian,
  49. word_bounds_source_code,
  50. );
  51. benchmark_main!(benches);