IAP GITLAB

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

New timer class added for cleaner structure

parent 0010da24
No related branches found
No related tags found
No related merge requests found
# create the library
add_library (CORSIKAanalytics INTERFACE)
# namespace of library -> location of header files
set (
CORSIKAanalytics_NAMESPACE
corsika/analytics
)
# header files of this library
set (
CORSIKAanalytics_HEADERS
Timer.h
)
# copy the headers into the namespace
CORSIKA_COPY_HEADERS_TO_NAMESPACE (CORSIKAanalytics
${CORSIKAanalytics_NAMESPACE}
${CORSIKAanalytics_HEADERS}
)
# include directive for upstream code
target_include_directories (
CORSIKAanalytics
INTERFACE
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include/>
C8::ext:boost
)
# and link against spdlog
#target_link_libraries(
# CORSIKAanalytics
# INTERFACE
# spdlog::spdlog
#)
# install library
install (
FILES ${CORSIKAanalytics_HEADERS}
DESTINATION include/${CORSIKAanalytics_NAMESPACE}
)
# ----------------
# code unit testing
CORSIKA_ADD_TEST (testAnalytics)
target_link_libraries (
testAnalytics
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;
}
}
#add_subdirectory (Analytics) add_subdirectory (Analytics)
add_subdirectory (Cascade) add_subdirectory (Cascade)
add_subdirectory (Geometry) add_subdirectory (Geometry)
add_subdirectory (Logging) add_subdirectory (Logging)
......
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