Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 73 lines (64 sloc) 2.02 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// Copyright 2012-2013 The Ephenation Authors
//
// This file is part of Ephenation.
//
// Ephenation 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, version 3.
//
// Ephenation 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 Ephenation. If not, see <http://www.gnu.org/licenses/>.
//

#pragma once

#include <string>
#include <GL/glfw.h>

#include "primitives.h"

/// @brief Measure real worst execution time, and provide a report.
/// Surround a section of code as follows:
/// @code{.cpp}
/// static WorstTime tm("name");
/// tm.Start();
/// SomeFunctionTakingTime();
/// tm.Stop();
/// @endcode
/// From the main loop, call WorstTime::Report() now and then.
///
class WorstTime {
public:
    /// @brief Declare a timer
    /// @param title A string that names the timer, and will be included in the final report
WorstTime(const std::string &title) : fStart(0.0), fIndex(0) {
if (!gDebugOpenGL || fLast == MAXSIZE)
return; // Quietly ignore
fIndex = fLast++; // Allocate a slot
fTitles[fIndex] = title;
}

/// @brief Start the timer
void Start() {
if (!gDebugOpenGL)
return;
fStart = glfwGetTime();
}

/// @brief Stop the timer. The worst case since the last report is saved.
void Stop(void) {
if (!gDebugOpenGL)
return;
double delta = glfwGetTime() - fStart;
if (delta > fResults[fIndex])
fResults[fIndex] = delta;
}

    /// @brief Report all timers and reset worst case.
static void Report(void);

private:
enum { MAXSIZE = 20 }; /// @todo Use std::vector instead!
double fStart;
static double fResults[MAXSIZE];
static std::string fTitles[MAXSIZE];
static unsigned fLast;

int fIndex;
};
Something went wrong with that request. Please try again.