numeric_literal_benchmark.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "common/check.h"
  6. #include "toolchain/diagnostics/null_diagnostics.h"
  7. #include "toolchain/lex/numeric_literal.h"
  8. namespace Carbon::Testing {
  9. namespace {
  10. using Lex::NumericLiteral;
  11. static void BM_Lex_Float(benchmark::State& state) {
  12. for (auto _ : state) {
  13. CARBON_CHECK(NumericLiteral::Lex("0.000001"));
  14. }
  15. }
  16. static void BM_Lex_Integer(benchmark::State& state) {
  17. for (auto _ : state) {
  18. CARBON_CHECK(NumericLiteral::Lex("1_234_567_890"));
  19. }
  20. }
  21. static void BM_ComputeValue_Float(benchmark::State& state) {
  22. auto val = NumericLiteral::Lex("0.000001");
  23. CARBON_CHECK(val);
  24. auto emitter = NullDiagnosticEmitter<const char*>();
  25. for (auto _ : state) {
  26. val->ComputeValue(emitter);
  27. }
  28. }
  29. static void BM_ComputeValue_Integer(benchmark::State& state) {
  30. auto val = NumericLiteral::Lex("1_234_567_890");
  31. auto emitter = NullDiagnosticEmitter<const char*>();
  32. CARBON_CHECK(val);
  33. for (auto _ : state) {
  34. val->ComputeValue(emitter);
  35. }
  36. }
  37. BENCHMARK(BM_Lex_Float);
  38. BENCHMARK(BM_Lex_Integer);
  39. BENCHMARK(BM_ComputeValue_Float);
  40. BENCHMARK(BM_ComputeValue_Integer);
  41. } // namespace
  42. } // namespace Carbon::Testing