Echo Writes Code

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "crucible/boot.hpp"
#include "crucible/core.hpp"

#include <chrono>
#include <cmath>
#include <iostream>
#include <string>

namespace crucible::boot::main
{
  constexpr std::size_t BENCHMARK_ITERATIONS { 10'000'000 };

  constexpr char const BULLET[] { "•" };

  constexpr char const GREEN[] { "\x1b[0;32m" };

  constexpr char const MAGENTA[] { "\x1b[0;35m" };

  constexpr char const BRIGHT[] { "\x1b[1m" };

  constexpr char const RESET[] { "\x1b[0m" };

  [[nodiscard]] auto replicate(std::string const &pattern, std::size_t const amount) -> std::string
  {
    std::string result {};

    for (std::size_t i { 0 }; i < amount; ++i) {
      result += pattern;
    }

    return result;
  }

  auto benchmark_core_math() -> void
  {
    {
      float value { 0.0f };
      float const delta { 0.000001f };
      [[maybe_unused]] float volatile storage { 0.0f };

      auto const t0 { std::chrono::steady_clock::now() };

      for (std::size_t i { 0 }; i < BENCHMARK_ITERATIONS; ++i) {
        storage = core::math::square_root(value);
        value += delta;
      }

      auto const t1 { std::chrono::steady_clock::now() };
      auto const ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();

      std::cout << BULLET << " " << MAGENTA << "crucible::core::math::square_root<float>()" << RESET << ": " << GREEN << BENCHMARK_ITERATIONS << RESET << " iterations; " << GREEN << ms << RESET << "ms\n";
    }

    {
      float value { 0.0f };
      float const delta { 0.000001f };
      [[maybe_unused]] float volatile storage { 0.0f };

      auto const t0 { std::chrono::steady_clock::now() };

      for (std::size_t i { 0 }; i < BENCHMARK_ITERATIONS; ++i) {
        storage = std::sqrt(value);
        value += delta;
      }

      auto const t1 { std::chrono::steady_clock::now() };
      auto const ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();

      std::cout << BULLET << " " << MAGENTA << "std::sqrt()" << RESET << ": " << GREEN << BENCHMARK_ITERATIONS << RESET << " iterations; " << GREEN << ms << RESET << "ms\n";
    }
  }

  auto launch(core::arrays::ImmutableSlice<std::string> const arguments) -> ExitStatus
  {
    static_cast<void>(arguments);

    std::string const full_header { "crucible-benchmark v" CRUCIBLE_VERSION };
    std::size_t const space_required { full_header.size() + 2 };
    std::string const horizontal_bars { replicate("─", space_required) };

    std::cout
      << "\n"
      << BRIGHT << "╭" << horizontal_bars << "╮\n" << RESET
      << BRIGHT << "│ " << full_header << " │\n" << RESET
      << BRIGHT << "╰" << horizontal_bars << "╯\n" << RESET
      << "\n"
      << BRIGHT << "Benchmarks\n" << RESET
      << BRIGHT << "──────────\n" << RESET
      << "\n";

    benchmark_core_math();

    return ExitStatus::Success;
  }
}