profiler.h
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2001-2002 Vivid Solutions Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#pragma once
#include <geos/export.h>
#include <chrono>
#include <map>
#include <memory>
#include <iostream>
#include <string>
#include <vector>
#ifndef PROFILE
#define PROFILE 0
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
#endif
namespace geos {
namespace util {
/*
* \class Profile utils.h geos.h
*
* \brief Profile statistics
*/
class GEOS_DLL Profile {
public:
using timeunit = std::chrono::microseconds;
Profile(std::string name);
~Profile() = default;
void
start()
{
starttime = std::chrono::high_resolution_clock::now();
}
void
stop()
{
stoptime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<timeunit>(stoptime - starttime);
timings.push_back(elapsed);
totaltime += elapsed;
if(timings.size() == 1) {
max = min = elapsed;
}
else {
if(elapsed > max) {
max = elapsed;
}
if(elapsed < min) {
min = elapsed;
}
}
avg = static_cast<double>(totaltime.count()) / static_cast<double>(timings.size());
}
double getMax() const;
double getMin() const;
double getTot() const;
std::string getTotFormatted() const;
double getAvg() const;
std::size_t getNumTimings() const;
std::string name;
private:
/* \brief current start and stop times */
std::chrono::high_resolution_clock::time_point starttime, stoptime;
/* \brief actual times */
std::vector<timeunit> timings;
/* \brief total time */
timeunit totaltime;
/* \brief max time */
timeunit max;
/* \brief max time */
timeunit min;
/* \brief avg time */
double avg;
};
/*
* \class Profiler utils.h geos.h
*
* \brief Profiling class
*
*/
class GEOS_DLL Profiler {
public:
Profiler() = default;
~Profiler() = default;
Profiler(const Profiler&) = delete;
Profiler& operator=(const Profiler&) = delete;
static Profiler* instance(void);
void start(std::string name);
void stop(std::string name);
Profile* get(std::string name);
std::map<std::string, std::unique_ptr<Profile>> profs;
};
GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profile&);
GEOS_DLL std::ostream& operator<< (std::ostream& os, const Profiler&);
} // namespace geos::util
} // namespace geos
#ifdef _MSC_VER
#pragma warning(pop)
#endif
geos::util
Utility classes for GEOS.
Definition namespaces.h:314
geos::util::operator<<
std::ostream & operator<<(std::ostream &os, const Profile &)
Return a string representing the Profile.
geos
Basic namespace for all GEOS functionalities.
Definition geos.h:38