Skip to content
Snippets Groups Projects
Commit 2aa72b93 authored by Philippe Gorley's avatar Philippe Gorley Committed by Adrien Béraud
Browse files

debug: add timer

Adds a Timer object to easily benchmark code.

Should be used only for debugging code, so a warning is issued if the
header is included.

Change-Id: I89bc5454fc0c182a50b14cafd205ee116b4a167e
parent 1b5bbb0d
No related branches found
No related tags found
No related merge requests found
/*
* Copyright (C) 2018 Savoir-faire Linux Inc.
*
* Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com>
*
* 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "config.h"
#include <chrono>
#include <ratio>
#warning Debug utilities included in build
using Clock = std::chrono::steady_clock;
namespace ring { namespace debug {
/**
* Ex:
* Timer t;
* std::this_thread::sleep_for(std::chrono::milliseconds(10));
* RING_DBG() << "Task took " << t.getDuration<std::chrono::nanoseconds>() << " ns";
*/
class Timer
{
public:
Timer() { start_ = Clock::now(); }
template<class Period = std::ratio<1>>
uint64_t getDuration()
{
auto diff = std::chrono::duration_cast<Period>(Clock::now() - start_);
return diff.count();
}
private:
std::chrono::time_point<Clock> start_;
};
}} // namespace ring::debug
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment