Files
itgmania212121/src/RageTimer.h
T

101 lines
4.9 KiB
C++
Raw Normal View History

2011-03-17 01:47:30 -04:00
/* RageTimer - Timer services. */
#ifndef RAGE_TIMER_H
#define RAGE_TIMER_H
2023-04-20 12:34:12 +02:00
#include <cstdint>
2011-03-17 01:47:30 -04:00
class RageTimer
{
public:
2024-04-24 02:31:03 -07:00
/* Initialize the m_secs and m_us values to 0 and then fill them with the current time. */
2011-03-17 01:47:30 -04:00
RageTimer(): m_secs(0), m_us(0) { Touch(); }
2024-04-24 02:31:03 -07:00
RageTimer( int64_t secs, int64_t us ): m_secs(secs), m_us(us) { }
2011-03-17 01:47:30 -04:00
/* Time ago this RageTimer represents. */
float Ago() const;
void Touch();
inline bool IsZero() const { return m_secs == 0 && m_us == 0; }
inline void SetZero() { m_secs = m_us = 0; }
/* Time between last call to GetDeltaTime() (Ago() + Touch()): */
float GetDeltaTime();
/* (alias) */
float PeekDeltaTime() const { return Ago(); }
2024-04-24 02:31:03 -07:00
static double GetTimeSinceStart( bool bAccurate = true ); // seconds since the program was started
2011-03-17 01:47:30 -04:00
static float GetTimeSinceStartFast() { return GetTimeSinceStart(false); }
2023-04-20 12:34:12 +02:00
static std::uint64_t GetUsecsSinceStart();
2011-03-17 01:47:30 -04:00
/* Get a timer representing half of the time ago as this one. */
RageTimer Half() const;
/* Add (or subtract) a duration from a timestamp. The result is another timestamp. */
RageTimer operator+( float tm ) const;
RageTimer operator-( float tm ) const { return *this + -tm; }
void operator+=( float tm ) { *this = *this + tm; }
void operator-=( float tm ) { *this = *this + -tm; }
/* Find the amount of time between two timestamps. The result is a duration. */
float operator-( const RageTimer &rhs ) const;
bool operator<( const RageTimer &rhs ) const;
2024-04-24 02:31:03 -07:00
/* The following is a "time since start" RageTimer. Splitting the seconds and
* microseconds values into two integers and combining them later allows for
* better precision. Use caution when changing data types, since resolution
* mismatch errors are easy to cause when changing things in RageTimer. */
std::uint64_t m_secs, m_us;
2011-03-17 01:47:30 -04:00
private:
static RageTimer Sum( const RageTimer &lhs, float tm );
2024-04-24 02:31:03 -07:00
static double Difference( const RageTimer &lhs, const RageTimer &rhs );
2011-03-17 01:47:30 -04:00
};
extern const RageTimer RageZeroTimer;
// For profiling how long some chunk of code takes. -Kyz
2023-04-20 12:34:12 +02:00
#define START_TIME(name) std::uint64_t name##_start_time= RageTimer::GetUsecsSinceStart();
2015-03-30 17:45:52 -06:00
#define START_TIME_CALL_COUNT(name) START_TIME(name); ++name##_call_count;
2023-04-20 12:34:12 +02:00
#define END_TIME(name) std::uint64_t name##_end_time= RageTimer::GetUsecsSinceStart(); LOG->Time(#name " time: %zu to %zu = %zu", name##_start_time, name##_end_time, name##_end_time - name##_start_time);
#define END_TIME_ADD_TO(name) std::uint64_t name##_end_time= RageTimer::GetUsecsSinceStart(); name##_total += name##_end_time - name##_start_time;
#define END_TIME_CALL_COUNT(name) END_TIME_ADD_TO(name); ++name##_end_count;
2023-04-20 12:34:12 +02:00
#define DECL_TOTAL_TIME(name) extern std::uint64_t name##_total;
#define DEF_TOTAL_TIME(name) std::uint64_t name##_total= 0;
2015-03-30 17:45:52 -06:00
#define PRINT_TOTAL_TIME(name) LOG->Time(#name " total time: %zu", name##_total);
2023-04-20 12:34:12 +02:00
#define DECL_TOT_CALL_PAIR(name) extern std::uint64_t name##_total; extern std::uint64_t name##_call_count;
#define DEF_TOT_CALL_PAIR(name) std::uint64_t name##_total= 0; std::uint64_t name##_call_count= 0;
#define PRINT_TOT_CALL_PAIR(name) LOG->Time(#name " calls: %zu, time: %zu, per: %f", name##_call_count, name##_total, static_cast<float>(name##_total) / name##_call_count);
2023-04-20 12:34:12 +02:00
#define DECL_TOT_CALL_END(name) DECL_TOT_CALL_PAIR(name); extern std::uint64_t name##_end_count;
#define DEF_TOT_CALL_END(name) DEF_TOT_CALL_PAIR(name); std::uint64_t name##_end_count= 0;
#define PRINT_TOT_CALL_END(name) LOG->Time(#name " calls: %zu, time: %zu, early end: %zu, per: %f", name##_call_count, name##_total, name##_end_count, static_cast<float>(name##_total) / (name##_call_count - name##_end_count));
2015-03-30 17:45:52 -06:00
2011-03-17 01:47:30 -04:00
#endif
/*
* Copyright (c) 2001-2003 Chris Danford, Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/