unicode_words.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 unicode_words(bench: &mut Bencher, path: &str) {
  8. let text = fs::read_to_string(path).unwrap();
  9. bench.iter(|| {
  10. for w in text.unicode_words() {
  11. bencher::black_box(w);
  12. }
  13. });
  14. bench.bytes = text.len() as u64;
  15. }
  16. fn unicode_words_arabic(bench: &mut Bencher) {
  17. unicode_words(bench, "benches/texts/arabic.txt");
  18. }
  19. fn unicode_words_english(bench: &mut Bencher) {
  20. unicode_words(bench, "benches/texts/english.txt");
  21. }
  22. fn unicode_words_hindi(bench: &mut Bencher) {
  23. unicode_words(bench, "benches/texts/hindi.txt");
  24. }
  25. fn unicode_words_japanese(bench: &mut Bencher) {
  26. unicode_words(bench, "benches/texts/japanese.txt");
  27. }
  28. fn unicode_words_korean(bench: &mut Bencher) {
  29. unicode_words(bench, "benches/texts/korean.txt");
  30. }
  31. fn unicode_words_mandarin(bench: &mut Bencher) {
  32. unicode_words(bench, "benches/texts/mandarin.txt");
  33. }
  34. fn unicode_words_russian(bench: &mut Bencher) {
  35. unicode_words(bench, "benches/texts/russian.txt");
  36. }
  37. fn unicode_words_source_code(bench: &mut Bencher) {
  38. unicode_words(bench, "benches/texts/source_code.txt");
  39. }
  40. benchmark_group!(
  41. benches,
  42. unicode_words_arabic,
  43. unicode_words_english,
  44. unicode_words_hindi,
  45. unicode_words_japanese,
  46. unicode_words_korean,
  47. unicode_words_mandarin,
  48. unicode_words_russian,
  49. unicode_words_source_code,
  50. );
  51. benchmark_main!(benches);