Hyphenator.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "minikin/Hyphenator.h"
  17. #include <benchmark/benchmark.h>
  18. #include "FileUtils.h"
  19. #include "UnicodeUtils.h"
  20. namespace minikin {
  21. const char* enUsHyph = "/system/usr/hyphen-data/hyph-en-us.hyb";
  22. const int enUsMinPrefix = 2;
  23. const int enUsMinSuffix = 3;
  24. static void BM_Hyphenator_short_word(benchmark::State& state) {
  25. Hyphenator* hyphenator = Hyphenator::loadBinary(readWholeFile(enUsHyph).data(), enUsMinPrefix,
  26. enUsMinSuffix, "en");
  27. std::vector<uint16_t> word = utf8ToUtf16("hyphen");
  28. std::vector<HyphenationType> result;
  29. while (state.KeepRunning()) {
  30. hyphenator->hyphenate(word, &result);
  31. }
  32. }
  33. // TODO: Use BENCHMARK_CAPTURE for parametrise.
  34. BENCHMARK(BM_Hyphenator_short_word);
  35. static void BM_Hyphenator_long_word(benchmark::State& state) {
  36. Hyphenator* hyphenator = Hyphenator::loadBinary(readWholeFile(enUsHyph).data(), enUsMinPrefix,
  37. enUsMinSuffix, "en");
  38. std::vector<uint16_t> word = utf8ToUtf16("Pneumonoultramicroscopicsilicovolcanoconiosis");
  39. std::vector<HyphenationType> result;
  40. while (state.KeepRunning()) {
  41. hyphenator->hyphenate(word, &result);
  42. }
  43. }
  44. // TODO: Use BENCHMARK_CAPTURE for parametrise.
  45. BENCHMARK(BM_Hyphenator_long_word);
  46. // TODO: Add more tests for other languages.
  47. } // namespace minikin