numeric_literal_benchmark.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <benchmark/benchmark.h>
  5. #include <string>
  6. #include "common/check.h"
  7. #include "toolchain/diagnostics/null_diagnostics.h"
  8. #include "toolchain/lex/numeric_literal.h"
  9. namespace Carbon::Lex {
  10. namespace {
  11. // Returns an integer literal string with `prefix` followed by `num_digits`
  12. // entries from `digits` (repeating `digits` as necessary).
  13. static auto MakeIntString(llvm::StringLiteral prefix, int radix, int num_digits)
  14. -> std::string {
  15. // Digits are reversed so that we can take `radix` digits from the end, and
  16. // never have 0 be the first digit.
  17. static constexpr llvm::StringLiteral Digits = "FEDCBA9876543210";
  18. std::string s;
  19. s.reserve(prefix.size() + num_digits);
  20. s.append(prefix);
  21. for (int i = 0; i < num_digits; i += radix) {
  22. s.append(Digits.take_back(std::min(radix, num_digits - i)));
  23. }
  24. return s;
  25. }
  26. static void BM_Lex_Float(benchmark::State& state) {
  27. for (auto _ : state) {
  28. CARBON_CHECK(NumericLiteral::Lex("0.000001", true));
  29. }
  30. }
  31. static void BM_Lex_Int(benchmark::State& state) {
  32. for (auto _ : state) {
  33. CARBON_CHECK(NumericLiteral::Lex("1_234_567_890", true));
  34. }
  35. }
  36. static void BM_Lex_IntDecimalN(benchmark::State& state) {
  37. std::string s = MakeIntString("", 10, state.range(0));
  38. for (auto _ : state) {
  39. CARBON_CHECK(NumericLiteral::Lex(s, true));
  40. }
  41. }
  42. static void BM_ComputeValue_Float(benchmark::State& state) {
  43. auto val = NumericLiteral::Lex("0.000001", true);
  44. CARBON_CHECK(val);
  45. auto& emitter = Diagnostics::NullEmitter<const char*>();
  46. for (auto _ : state) {
  47. val->ComputeValue(emitter);
  48. }
  49. }
  50. static void BM_ComputeValue_Int(benchmark::State& state) {
  51. auto val = NumericLiteral::Lex("1_234_567_890", true);
  52. auto& emitter = Diagnostics::NullEmitter<const char*>();
  53. CARBON_CHECK(val);
  54. for (auto _ : state) {
  55. val->ComputeValue(emitter);
  56. }
  57. }
  58. static void BM_ComputeValue_IntDecimalN(benchmark::State& state) {
  59. std::string s = MakeIntString("", 10, state.range(0));
  60. auto val = NumericLiteral::Lex(s, true);
  61. auto& emitter = Diagnostics::NullEmitter<const char*>();
  62. CARBON_CHECK(val);
  63. for (auto _ : state) {
  64. val->ComputeValue(emitter);
  65. }
  66. }
  67. static void BM_ComputeValue_IntBinaryN(benchmark::State& state) {
  68. std::string s = MakeIntString("0b", 2, state.range(0));
  69. auto val = NumericLiteral::Lex(s, true);
  70. auto& emitter = Diagnostics::NullEmitter<const char*>();
  71. CARBON_CHECK(val);
  72. for (auto _ : state) {
  73. val->ComputeValue(emitter);
  74. }
  75. }
  76. static void BM_ComputeValue_IntOctalN(benchmark::State& state) {
  77. std::string s = MakeIntString("0o", 8, state.range(0));
  78. auto val = NumericLiteral::Lex(s, true);
  79. auto& emitter = Diagnostics::NullEmitter<const char*>();
  80. CARBON_CHECK(val);
  81. for (auto _ : state) {
  82. val->ComputeValue(emitter);
  83. }
  84. }
  85. static void BM_ComputeValue_IntHexN(benchmark::State& state) {
  86. // 0 is in the middle so that it isn't truncated in parse.
  87. std::string s = MakeIntString("0x", 16, state.range(0));
  88. auto val = NumericLiteral::Lex(s, true);
  89. auto& emitter = Diagnostics::NullEmitter<const char*>();
  90. CARBON_CHECK(val);
  91. for (auto _ : state) {
  92. val->ComputeValue(emitter);
  93. }
  94. }
  95. BENCHMARK(BM_Lex_Float);
  96. BENCHMARK(BM_Lex_Int);
  97. BENCHMARK(BM_Lex_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
  98. BENCHMARK(BM_ComputeValue_Float);
  99. BENCHMARK(BM_ComputeValue_Int);
  100. BENCHMARK(BM_ComputeValue_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
  101. BENCHMARK(BM_ComputeValue_IntBinaryN)->RangeMultiplier(10)->Range(1, 10000);
  102. BENCHMARK(BM_ComputeValue_IntOctalN)->RangeMultiplier(10)->Range(1, 10000);
  103. BENCHMARK(BM_ComputeValue_IntHexN)->RangeMultiplier(10)->Range(1, 10000);
  104. } // namespace
  105. } // namespace Carbon::Lex