numeric_literal_benchmark.cpp 1.3 KB

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