From 2aa72b9302f5f0b2fa20c7dec78fdafeb6b07cd9 Mon Sep 17 00:00:00 2001 From: philippegorley <philippe.gorley@savoirfairelinux.com> Date: Wed, 4 Jul 2018 11:13:55 -0400 Subject: [PATCH] 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 --- src/debug_utils.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/debug_utils.h diff --git a/src/debug_utils.h b/src/debug_utils.h new file mode 100644 index 0000000000..b4d3d183e9 --- /dev/null +++ b/src/debug_utils.h @@ -0,0 +1,56 @@ +/* + * 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 -- GitLab