numeric_literal_benchmark.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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,
  14. llvm::StringLiteral digits, size_t num_digits)
  15. -> std::string {
  16. std::string s;
  17. s.reserve(prefix.size() + num_digits);
  18. s.append(prefix);
  19. for (size_t i = 0; i < num_digits; i += digits.size()) {
  20. s.append(digits.take_front(std::min(digits.size(), num_digits - i)));
  21. }
  22. return s;
  23. }
  24. static void BM_Lex_Float(benchmark::State& state) {
  25. for (auto _ : state) {
  26. CARBON_CHECK(NumericLiteral::Lex("0.000001", true));
  27. }
  28. }
  29. static void BM_Lex_Int(benchmark::State& state) {
  30. for (auto _ : state) {
  31. CARBON_CHECK(NumericLiteral::Lex("1_234_567_890", true));
  32. }
  33. }
  34. static void BM_Lex_IntDecimalN(benchmark::State& state) {
  35. std::string s = MakeIntString("", "1234567890", state.range(0));
  36. for (auto _ : state) {
  37. CARBON_CHECK(NumericLiteral::Lex(s, true));
  38. }
  39. }
  40. static void BM_ComputeValue_Float(benchmark::State& state) {
  41. auto val = NumericLiteral::Lex("0.000001", true);
  42. CARBON_CHECK(val);
  43. auto& emitter = Diagnostics::NullEmitter<const char*>();
  44. for (auto _ : state) {
  45. val->ComputeValue(emitter);
  46. }
  47. }
  48. static void BM_ComputeValue_Int(benchmark::State& state) {
  49. auto val = NumericLiteral::Lex("1_234_567_890", true);
  50. auto& emitter = Diagnostics::NullEmitter<const char*>();
  51. CARBON_CHECK(val);
  52. for (auto _ : state) {
  53. val->ComputeValue(emitter);
  54. }
  55. }
  56. static void BM_ComputeValue_IntDecimalN(benchmark::State& state) {
  57. std::string s = MakeIntString("", "1234567890", state.range(0));
  58. auto val = NumericLiteral::Lex(s, true);
  59. auto& emitter = Diagnostics::NullEmitter<const char*>();
  60. CARBON_CHECK(val);
  61. for (auto _ : state) {
  62. val->ComputeValue(emitter);
  63. }
  64. }
  65. static void BM_ComputeValue_IntBinaryN(benchmark::State& state) {
  66. std::string s = MakeIntString("0b", "10", state.range(0));
  67. auto val = NumericLiteral::Lex(s, true);
  68. auto& emitter = Diagnostics::NullEmitter<const char*>();
  69. CARBON_CHECK(val);
  70. for (auto _ : state) {
  71. val->ComputeValue(emitter);
  72. }
  73. }
  74. static void BM_ComputeValue_IntHexN(benchmark::State& state) {
  75. // 0 is in the middle so that it isn't truncated in parse.
  76. std::string s = MakeIntString("0x", "1234567890ABCDEF", state.range(0));
  77. auto val = NumericLiteral::Lex(s, true);
  78. auto& emitter = Diagnostics::NullEmitter<const char*>();
  79. CARBON_CHECK(val);
  80. for (auto _ : state) {
  81. val->ComputeValue(emitter);
  82. }
  83. }
  84. BENCHMARK(BM_Lex_Float);
  85. BENCHMARK(BM_Lex_Int);
  86. BENCHMARK(BM_Lex_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
  87. BENCHMARK(BM_ComputeValue_Float);
  88. BENCHMARK(BM_ComputeValue_Int);
  89. BENCHMARK(BM_ComputeValue_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
  90. BENCHMARK(BM_ComputeValue_IntBinaryN)->RangeMultiplier(10)->Range(1, 10000);
  91. BENCHMARK(BM_ComputeValue_IntHexN)->RangeMultiplier(10)->Range(1, 10000);
  92. } // namespace
  93. } // namespace Carbon::Lex