From 68c1bcc09562a091995b6bc51a48c5a7f689709e Mon Sep 17 00:00:00 2001 From: Sylvain Pion Date: Wed, 3 Dec 2008 23:05:35 +0000 Subject: [PATCH] New profiler, for profiling time spent in a function/block. --- .../Profiling_tools/Profile_timer.cpp | 15 ++++ .../include/CGAL/Profile_counter.h | 18 +++-- Profiling_tools/include/CGAL/Profile_timer.h | 76 +++++++++++++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 Profiling_tools/examples/Profiling_tools/Profile_timer.cpp create mode 100644 Profiling_tools/include/CGAL/Profile_timer.h diff --git a/Profiling_tools/examples/Profiling_tools/Profile_timer.cpp b/Profiling_tools/examples/Profiling_tools/Profile_timer.cpp new file mode 100644 index 00000000000..3fa7132d912 --- /dev/null +++ b/Profiling_tools/examples/Profiling_tools/Profile_timer.cpp @@ -0,0 +1,15 @@ +#define CGAL_PROFILE + +#include + +int main() +{ + CGAL_TIME_PROFILER("seconds spent in this for loop"); + for (int i=0; i<10; ++i) + { + // do something + double d = 1+1; + (void) d; + } + return 0; +} diff --git a/Profiling_tools/include/CGAL/Profile_counter.h b/Profiling_tools/include/CGAL/Profile_counter.h index 6b42042f9ec..2e3518f6ea6 100644 --- a/Profiling_tools/include/CGAL/Profile_counter.h +++ b/Profiling_tools/include/CGAL/Profile_counter.h @@ -21,21 +21,25 @@ #ifndef CGAL_PROFILE_COUNTER_H #define CGAL_PROFILE_COUNTER_H -// This file contains 3 classes to help in profiling, together with macros +// This file contains several classes to help in profiling, together with macros // triggered by CGAL_PROFILE to enable them: // // - Profile_counter which is able to keep track of a number, and prints a -// message in the destructor. Typically, it can be used as a profile counter -// in a static variable. +// message in the destructor. Typically, it can be used as a profile counter +// in a static variable. // // - Profile_histogram_counter which is similar, but the counter is indexed by -// a value (unsigned int), and the final dump is the histogram of the non-zero -// counters. +// a value (unsigned int), and the final dump is the histogram of the non-zero +// counters. // // - Profile_branch_counter which keeps track of 2 counters, aiming at measuring -// the ratio corresponding to the number of times a branch is taken. +// the ratio corresponding to the number of times a branch is taken. +// +// See also CGAL/Profile_timer.h -// TODO : complete the documentation. +// TODO : +// - Really complete the documentation! +// - Probably at some point we will need ways to selectively enable/disable profilers? #include #include diff --git a/Profiling_tools/include/CGAL/Profile_timer.h b/Profiling_tools/include/CGAL/Profile_timer.h new file mode 100644 index 00000000000..328e31f5909 --- /dev/null +++ b/Profiling_tools/include/CGAL/Profile_timer.h @@ -0,0 +1,76 @@ +// Copyright (c) 2008 INRIA Sophia-Antipolis (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; version 2.1 of the License. +// See the file LICENSE.LGPL distributed with CGAL. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// +// Author(s) : Sylvain Pion + +#ifndef CGAL_PROFILE_TIMER_H +#define CGAL_PROFILE_TIMER_H + +// This file contains a class to help in profiling, together with macros +// triggered by CGAL_PROFILE to enable it: +// +// - Profile_timer which keeps track of the time spent in its scope-block. +// (be careful at recursive functions :-). +// +// See also CGAL/Profile_counter.h + +#include +#include +#include +#include +#include + +CGAL_BEGIN_NAMESPACE + +struct Profile_timer +{ + class Local { + Profile_timer &p; + public: + Local(Profile_timer& p_) : p(p_) { p.start(); } + ~Local() { p.stop(); } + }; + + Profile_timer(const std::string & ss) + : s(ss) { t.reset(); } + + void start() { t.start(); } + void stop() { t.stop(); } + + ~Profile_timer() + { + std::cerr << "[CGAL::Profile_timer] " + << std::setw(10) << t.time() << " " << s << std::endl; + } + +private: + Real_timer t; + const std::string s; +}; + + +#ifdef CGAL_PROFILE +# define CGAL_TIME_PROFILER(NAME) \ + static CGAL::Profile_timer CGAL_profile_timer_tmp(NAME); \ + CGAL::Profile_timer::Local CGAL_local_profile_timer_tmp(CGAL_profile_timer_tmp); +#else +# define CGAL_TIME_PROFILER(NAME) +#endif + +CGAL_END_NAMESPACE + +#endif // CGAL_PROFILE_TIMER_H