Skip to content

Commit

Permalink
time.hpp : add std::chrono::duration<double, std::ratio<1>> duration_…
Browse files Browse the repository at this point in the history
…in_s()
  • Loading branch information
pthom committed Oct 10, 2018
1 parent 70caa0d commit eaf05ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 9 additions & 3 deletions include/fplus/timed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ namespace fplus
timed() : base_pair() {}
timed(const T& val, ExecutionTime t = 0.) : base_pair(val, t) {}

// Execution time in seconds
// Execution time in seconds (returns a double)
ExecutionTime time_in_s() const { return base_pair::second; }
// Execution time as a std::chrono::duration<double>
std::chrono::duration<double, std::ratio<1>> duration_in_s() const
{
return std::chrono::duration<double, std::ratio<1>>(time_in_s());
}

// Inner value
const T& get() const { return base_pair::first; }
T& get() { return base_pair::first; }
const T& get() const { return base_pair::first; }
T& get() { return base_pair::first; }
};


Expand Down
11 changes: 10 additions & 1 deletion test/timed_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ namespace
void void_function()
{
sleep_seconds(0.002);
std::cout << "void_function" << std::endl;
}
}

Expand Down Expand Up @@ -96,6 +95,16 @@ TEST_CASE("timed, show_timed")
}
}

TEST_CASE("timed, duration_in_s")
{
{
fplus::timed<int> v(42, 1.2345);
auto d = v.duration_in_s();
double seconds = d.count();
REQUIRE( seconds == doctest::Approx(1.2345) );
}
}


// Test make_timed_function
TEST_CASE("make_timed_function")
Expand Down

0 comments on commit eaf05ef

Please sign in to comment.