Skip to content

Commit

Permalink
Add timer wrapper to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
Ergus committed Mar 3, 2024
1 parent de39dde commit a02b9cc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
24 changes: 4 additions & 20 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,10 @@ jobs:
fail-fast: false
matrix:
config:
- {
os: ubuntu-latest,
cc: "gcc",
cxx: "g++"
}
- {
os: ubuntu-latest,
cc: "clang",
cxx: "clang++"
}
- {
os: macos-latest,
cc: "clang",
cxx: "clang++"
}
- {
os: windows-latest,
cc: "cl",
cxx: "cl"
}
- {os: ubuntu-latest, cc: "gcc", cxx: "g++"}
- {os: ubuntu-latest, cc: "clang", cxx: "clang++"}
- {os: macos-latest, cc: "clang", cxx: "clang++"}
- {os: windows-latest, cc: "cl", cxx: "cl"}

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 0 additions & 1 deletion argparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ typedef struct timer {

timer create_timer(const char *name);
double getNS_timer(const timer *in);
void print_timer(const timer *in);
void start_timer(timer *out);
void stop_timer(timer *out);
void reset_timer(timer *out);
Expand Down
20 changes: 17 additions & 3 deletions argparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ namespace {

template <typename T>
struct trait{
using type = T;
static type convert(const T& in)
static T convert(const T& in)
{
return in;
}
};

template <>
struct trait<std::string>{
using type = const char*;
static const char* convert(const std::string& in)
{
return in.c_str();
Expand Down Expand Up @@ -78,6 +76,7 @@ namespace {

operator T() { return _value; }
};

}

namespace argparser {
Expand Down Expand Up @@ -107,4 +106,19 @@ namespace argparser {
int get_rest(char **rest) {
return get_rest_args(&rest);
}

class time {
timer t;

public:
explicit time(const std::string& name)
: t(create_timer(name.c_str()))
{}

double getNS() const { return getNS_timer(&t); }
void start() { start_timer(&t); }
void stop() { stop_timer(&t); }
void reset() { reset_timer(&t); }
};

};
65 changes: 36 additions & 29 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include <iostream>
#include "argparser.hpp"

#ifdef _MSC_VER
#include <Windows.h>
#define sleep(X) Sleep(X * 1000)
#else
#include <unistd.h>
#endif

const std::string global_string = "Value for reportable in global variable";

Expand Down Expand Up @@ -63,20 +69,20 @@ int main(int argc, char *argv[])
std::cout << "# Read: o_string_3 " << o_string_3 << std::endl;
assert(o_string_3 == ((argc > 8 ? argv[it++] : "opt1 opt2")));

// timer t1 = create_timer("timer_1");
// int r_int_1 = create_reportable_int("r_int_1", argc);
// printf("# Report: r_int_1 %d\n", r_int_1);
// assert(r_int_1 == argc);

// char **rest = NULL;
// int nrest = get_rest_args(&rest);
// if (nrest > 0){
// printf("# ");
// for (int i = 0; i < nrest; ++i) {
// printf("rest[%d]: \"%s\", ", i, rest[i]);
// }
// printf("\n");
// }
argparser::time t1("timer_1");
int r_int_1 = argparser::reportable<int>("r_int_1", argc);
printf("# Report: r_int_1 %d\n", r_int_1);
assert(r_int_1 == argc);

char **rest = NULL;
int nrest = get_rest_args(&rest);
if (nrest > 0){
printf("# ");
for (int i = 0; i < nrest; ++i) {
printf("rest[%d]: \"%s\", ", i, rest[i]);
}
printf("\n");
}

double r_double_1 = argparser::reportable<double>("r_double_1", v_double_1 / o_double_1);
std::cout << "# Report: r_double_1 " << r_double_1 << std::endl;
Expand All @@ -94,29 +100,30 @@ int main(int argc, char *argv[])
std::cout << "# Report: r_string_3 " << r_string_3 << std::endl;
assert(r_string_3 == global_string);

// timer t2 = create_timer("timer_2"); // New timer to trigger realloc
// printf("# Start sleep\n");
// sleep(1);
// printf("# End sleep\n");
// stop_timer(&t2);
argparser::time t2("timer_2"); // New timer to trigger realloc
std::cout << "# Start sleep" << std::endl;
sleep(1);
std::cout << "# End sleep\n" << std::endl;
t2.stop();

// const double diff_time1 = getNS_timer(&t1);
// printf("# Timer: T1 %lf\n", diff_time1);
// assert(diff_time1 == 0.);
const double diff_time1 = t1.getNS();
printf("# Timer: T1 %lf\n", diff_time1);
assert(diff_time1 == 0.);

// const double diff_time2 = getNS_timer(&t2);
// printf("# Times: T2 %lf\n", diff_time2);
// assert(diff_time2 >= 1E9);
const double diff_time2 = t2.getNS();
printf("# Times: T2 %lf\n", diff_time2);
assert(diff_time2 >= 1E9);

// reset_timer(&t2);
// const double diff_time_reset = getNS_timer(&t2);
// printf("# Times: T2 (reset) %lf\n", diff_time_reset);
// assert(diff_time_reset == 0.);
t2.reset();
const double diff_time_reset = t2.getNS();
printf("# Times: T2 (reset) %lf\n", diff_time_reset);
assert(diff_time_reset == 0.);

std::cout << "# Reporting args" << std::endl;
argparser::report<>();

std::cout << "\n# Call free args" << std::endl;

argparser::free();
return 0;
}

0 comments on commit a02b9cc

Please sign in to comment.