IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 9048e0aa authored by Dominik Baack's avatar Dominik Baack Committed by Ralf Ulrich
Browse files

New timer class added for cleaner structure

parent 73f05c1c
No related branches found
No related tags found
1 merge request!253Add benchmark processor and dummy processors
......@@ -45,6 +45,7 @@ install (
# ----------------
# code unit testing
<<<<<<< HEAD
CORSIKA_ADD_TEST (testFunctionTimer)
target_link_libraries (
testFunctionTimer
......@@ -55,6 +56,11 @@ target_link_libraries (
CORSIKA_ADD_TEST (testClassTimer)
target_link_libraries (
testClassTimer
=======
CORSIKA_ADD_TEST (testAnalytics)
target_link_libraries (
testAnalytics
>>>>>>> New timer class added for cleaner structure
CORSIKAanalytics
CORSIKAtesting
)
/*
* (c) Copyright 2019 CORSIKA Project, corsika-project@lists.kit.edu
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
*/
// Another possibility:
// https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Execute-Around_Pointer
#pragma once
#include <chrono>
#include <utility>
namespace corsika::analytics {
template <typename TFunc, typename TClock = std::chrono::high_resolution_clock,
typename TDuration = std::chrono::microseconds>
class timeFunction {
private:
typename TClock::time_point vStart;
TDuration vDiff;
TFunc vFunction;
public:
timeFunction(TFunc f)
: vFunction(f) {}
template <typename... TArgs>
auto operator()(TArgs&&... args) -> std::invoke_result_t<TFunc, TArgs...> {
vStart = TClock::now();
auto tmp = vFunction(std::forward<TArgs>(args)...);
vDiff = std::chrono::duration_cast<TDuration>(TClock::now() - vStart);
return tmp;
}
inline TDuration getTime() const { return vDiff; }
};
template <typename TClass, typename TClock = std::chrono::high_resolution_clock,
typename TDuration = std::chrono::microseconds>
class timeProxy : public TClass {
private:
typename TClock::time_point vStart;
TDuration vDiff;
TClass& vObj;
/*template <typename F, typename... Args>
decltype(auto) call_func(F func, Args&&... args) {
return (vObj.*func)(std::forward<Args>(args)...);
}*/
public:
template<typename ... TArgs>
timeProxy(TArgs args) : TClass<TArgs...>(std::forward<TArgs>(args)...)
{}
auto operator->() {return 2;}
inline TDuration getTime() const { return vDiff; }
};
} // namespace corsika::analytics
/*
* (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
*/
#include <corsika/analytics/Timer.h>
#include <catch2/catch.hpp>
#include <chrono>
#include <iostream>
#include <thread>
using namespace corsika;
int testFunc() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return 31415;
}
class testClass {
public:
int testFunc() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return 31415;
}
int operator()() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return 31415;
}
};
TEST_CASE("Analytics", "[Timer]") {
SECTION("Measure runtime of a free function") {
auto test = corsika::analytics::timeFunction(testFunc);
std::cout << test() << std::endl;
std::cout << test.getTime().count() << std::endl;
}
SECTION("Measure runtime of a class functor") {
testClass testC;
auto test = corsika::analytics::timeFunction(testC);
std::cout << test() << std::endl;
std::cout << test.getTime().count() << std::endl;
}
SECTION("Measure runtime of a class member function") {
testClass testC;
auto test = corsika::analytics::timeProxy<testClass>();
std::cout << test() << std::endl;
std::cout << test.getTime().count() << std::endl;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment