Skip to content

Commit

Permalink
[cpp] print out timings upon success
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed May 5, 2024
1 parent cf635c2 commit d254f4b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ add_executable(psyc
src/lex.cpp
src/lex.hpp
src/srcloc.hpp
src/timer.cpp
src/timer.hpp
src/util.cpp
src/util.hpp
)
Expand Down
22 changes: 22 additions & 0 deletions cpp/src/psyc_main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
#include "config.hpp"
#include "lex.hpp"
#include "timer.hpp"
#include "diag.hpp"
#include <filesystem>
#include <span>
#include <string_view>
#include <iomanip>

config::compiler_args parse_args(std::span<const std::string_view> cli_args);
void print_version_info();

struct timers
{
std::uint64_t lexing = 0u;
std::uint64_t parsing = 0u;
std::uint64_t semal = 0u;
std::uint64_t codegen = 0u;
void print()
{
constexpr int width = 16;
std::cout << std::setw(width) << "lexer:" << std::setw(6) << std::setprecision(3) << (this->lexing / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "parser:" << std::setw(6) << std::setprecision(3) << (this->parsing / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "semal:" << std::setw(6) << std::setprecision(3) << (this->semal / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "codegen:" << std::setw(6) << std::setprecision(3) << (this->codegen / 1000.0f) << " seconds" << std::endl;
}
};

int main(int argc, char** argv)
{
const std::vector<std::string_view> cli_args(argv + 1, argv + argc);
config::compiler_args args = parse_args(cli_args);
timers t;

timer::start();
lex::state lex;
for(const std::filesystem::path input_file : args.input_files)
{
lex.tokenised_input_files[input_file] = lex::tokenise(input_file);
}
t.lexing = timer::elapsed_millis();

t.print();
return 0;
}

Expand Down
18 changes: 18 additions & 0 deletions cpp/src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "timer.hpp"
#include <chrono>

namespace timer
{
std::chrono::time_point<std::chrono::system_clock> now;

void start()
{
now = std::chrono::system_clock::now();
}

std::uint64_t elapsed_millis()
{
auto right_now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
return right_now - std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
}
}
8 changes: 8 additions & 0 deletions cpp/src/timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cstdint>

namespace timer
{
void start();

std::uint64_t elapsed_millis();
}

0 comments on commit d254f4b

Please sign in to comment.