Skip to content

Commit

Permalink
Timer: Add a timer for code-benchmarking with RAII
Browse files Browse the repository at this point in the history
Closes #555
  • Loading branch information
insunaa authored and killerwife committed Nov 12, 2024
1 parent 2f5d3f4 commit e92c4c4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/shared/Util/CodeBench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "CodeBench.h"
#include "Log/Log.h"

ChronoTimeTracker::~ChronoTimeTracker()
{
std::chrono::nanoseconds elapsed = elapsedNanos();
sLog.outError("%s: time elapsed: %ldns, %ldµs, %ldms, %lds",
m_name.c_str(), nanos(elapsed).count(), micros(elapsed).count(), millis(elapsed).count(), secs(elapsed).count());
}
53 changes: 53 additions & 0 deletions src/shared/Util/CodeBench.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef MANGOS_TIMER_H
#define MANGOS_TIMER_H

struct ChronoTimeTracker
{
public:
ChronoTimeTracker() : m_startTime(std::chrono::steady_clock::now()), m_name("Unnamed Timer") {}
ChronoTimeTracker(std::string name) : m_startTime(std::chrono::steady_clock::now()), m_name(name) {}
~ChronoTimeTracker();
std::chrono::seconds elapsedSeconds()
{
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::milliseconds elapsedMillis()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::microseconds elapsedMicros()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::nanoseconds elapsedNanos()
{
return std::chrono::steady_clock::now() - m_startTime;
}
private:
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> m_startTime;
std::string m_name;
inline constexpr std::chrono::nanoseconds nanos(std::chrono::nanoseconds nanos) { return nanos; }
inline constexpr std::chrono::microseconds micros(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::microseconds>(nanos); }
inline constexpr std::chrono::milliseconds millis(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::milliseconds>(nanos); }
inline constexpr std::chrono::seconds secs(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::seconds>(nanos); }
};

#endif

0 comments on commit e92c4c4

Please sign in to comment.