AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
TimerBlock.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agx/Timer.h>
20#include <agx/String.h>
21
22namespace agxUtil
23{
29 {
30 public:
31 using OnDeleteCallback = std::function<void( agx::UInt64, agx::UInt64 )>;
32
33 public:
51 template<typename... ArgsT>
52 static TimerBlock create( const agx::String& format, ArgsT&&... args )
53 {
54 agx::String printout = agx::String::format( format.c_str(), std::forward<ArgsT>( args )... );
55 return TimerBlock( [printout]( agx::UInt64 start, agx::UInt64 stop )
56 {
57 std::cout << "{Timer Block}: " << printout << std::endl;
58 std::cout << " - Execute time: " << agx::Timer::convertToMs( stop - start ) << " ms." << std::endl;
59 } );
60 }
61
62 public:
68 inline TimerBlock( OnDeleteCallback onDeleteCallback )
69 : m_onDeleteCallback( onDeleteCallback ),
70 m_start( agx::Timer::getCurrentTick() ),
71 m_stop( agx::InvalidIndex )
72 {
73 }
74
78 inline ~TimerBlock()
79 {
80 if ( m_stop == agx::InvalidIndex )
81 stop();
82
83 if ( m_onDeleteCallback )
84 m_onDeleteCallback( m_start, m_stop );
85 }
86
90 inline void stop()
91 {
93 }
94
95 private:
96 OnDeleteCallback m_onDeleteCallback;
97 agx::UInt64 m_start;
98 agx::UInt64 m_stop;
99 };
100}
Utility class to time a scope.
Definition: TimerBlock.h:29
static TimerBlock create(const agx::String &format, ArgsT &&... args)
Create timer block given format string.
Definition: TimerBlock.h:52
TimerBlock(OnDeleteCallback onDeleteCallback)
Construct given title.
Definition: TimerBlock.h:68
~TimerBlock()
Print during destruction.
Definition: TimerBlock.h:78
std::function< void(agx::UInt64, agx::UInt64)> OnDeleteCallback
Definition: TimerBlock.h:31
void stop()
Stop the timer before we leave the scope.
Definition: TimerBlock.h:90
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:471
static UInt64 getCurrentTick()
Definition: Timer.h:207
static Real64 convertToMs(UInt64 ticks)
Definition: Timer.h:251
The agxUtil namespace contain classes and methods for utility functionality.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint64_t UInt64
Definition: Integer.h:33
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex