BWAPI
|
00001 // Copyright (c) 2005,2006,2008 INRIA Sophia-Antipolis (France). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation; version 2.1 of the License. 00007 // See the file LICENSE.LGPL distributed with CGAL. 00008 // 00009 // Licensees holding a valid commercial license may use this file in 00010 // accordance with the commercial license agreement provided with the software. 00011 // 00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 // 00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Profiling_tools/include/CGAL/Profile_counter.h $ 00016 // $Id: Profile_counter.h 47566 2008-12-20 17:31:04Z spion $ 00017 // 00018 // 00019 // Author(s) : Sylvain Pion 00020 00021 #ifndef CGAL_PROFILE_COUNTER_H 00022 #define CGAL_PROFILE_COUNTER_H 00023 00024 // This file contains several classes to help in profiling, together with macros 00025 // triggered by CGAL_PROFILE to enable them: 00026 // 00027 // - Profile_counter which is able to keep track of a number, and prints a 00028 // message in the destructor. Typically, it can be used as a profile counter 00029 // in a static variable. 00030 // 00031 // - Profile_histogram_counter which is similar, but the counter is indexed by 00032 // a value (unsigned int), and the final dump is the histogram of the non-zero 00033 // counters. 00034 // 00035 // - Profile_branch_counter which keeps track of 2 counters, aiming at measuring 00036 // the ratio corresponding to the number of times a branch is taken. 00037 // 00038 // - Profile_branch_counter_3 which keeps track of 3 counters, aiming at measuring 00039 // the ratios corresponding to the number of times 2 branches are taken. 00040 // 00041 // See also CGAL/Profile_timer.h 00042 00043 // TODO : 00044 // - Really complete the documentation! 00045 // - Probably at some point we will need ways to selectively enable/disable profilers? 00046 // (kind-wise and/or place-wise) 00047 // - Ideas for new kinds of profilers: 00048 // - lock counters in parallel mode 00049 // (e.g. time spent spinning, and/or number of locks taken or forbidden...) 00050 00051 #include <CGAL/config.h> 00052 #include <iostream> 00053 #include <iomanip> 00054 #include <string> 00055 #include <map> 00056 00057 CGAL_BEGIN_NAMESPACE 00058 00059 struct Profile_counter 00060 { 00061 Profile_counter(const std::string & ss) 00062 : i(0), s(ss) {} 00063 00064 void operator++() { ++i; } 00065 00066 ~Profile_counter() 00067 { 00068 std::cerr << "[CGAL::Profile_counter] " 00069 << std::setw(10) << i << " " << s << std::endl; 00070 } 00071 00072 private: 00073 unsigned int i; 00074 const std::string s; 00075 }; 00076 00077 00078 00079 struct Profile_histogram_counter 00080 { 00081 Profile_histogram_counter(const std::string & ss) 00082 : s(ss) {} 00083 00084 void operator()(unsigned i) { ++counters[i]; } 00085 00086 ~Profile_histogram_counter() 00087 { 00088 unsigned total=0; 00089 for (Counters::const_iterator it=counters.begin(), end=counters.end(); 00090 it != end; ++it) { 00091 std::cerr << "[CGAL::Profile_histogram_counter] " << s; 00092 std::cerr << " [ " << std::setw(10) << it->first << " : " 00093 << std::setw(10) << it->second << " ]" 00094 << std::endl; 00095 total += it->second; 00096 } 00097 std::cerr << "[CGAL::Profile_histogram_counter] " << s; 00098 std::cerr << " [ " << std::setw(10) << "Total" << " : " 00099 << std::setw(10) << total << " ]" << std::endl; 00100 } 00101 00102 private: 00103 typedef std::map<unsigned, unsigned> Counters; 00104 Counters counters; 00105 const std::string s; 00106 }; 00107 00108 00109 struct Profile_branch_counter 00110 { 00111 Profile_branch_counter(const std::string & ss) 00112 : i(0), j(0), s(ss) {} 00113 00114 void operator++() { ++i; } 00115 00116 void increment_branch() { ++j; } 00117 00118 ~Profile_branch_counter() 00119 { 00120 std::cerr << "[CGAL::Profile_branch_counter] " 00121 << std::setw(10) << j << " / " 00122 << std::setw(10) << i << " " << s << std::endl; 00123 } 00124 00125 private: 00126 unsigned int i, j; 00127 const std::string s; 00128 }; 00129 00130 00131 struct Profile_branch_counter_3 00132 { 00133 Profile_branch_counter_3(const std::string & ss) 00134 : i(0), j(0), k(0), s(ss) {} 00135 00136 void operator++() { ++i; } 00137 00138 void increment_branch_1() { ++j; } 00139 void increment_branch_2() { ++k; } 00140 00141 ~Profile_branch_counter_3() 00142 { 00143 std::cerr << "[CGAL::Profile_branch_counter_3] " 00144 << std::setw(10) << k << " / " 00145 << std::setw(10) << j << " / " 00146 << std::setw(10) << i << " " << s << std::endl; 00147 } 00148 00149 private: 00150 unsigned int i, j, k; 00151 const std::string s; 00152 }; 00153 00154 00155 #ifdef CGAL_PROFILE 00156 # define CGAL_PROFILER(Y) \ 00157 { static CGAL::Profile_counter tmp(Y); ++tmp; } 00158 # define CGAL_HISTOGRAM_PROFILER(Y, Z) \ 00159 { static CGAL::Profile_histogram_counter tmp(Y); tmp(Z); } 00160 # define CGAL_BRANCH_PROFILER(Y, NAME) \ 00161 static CGAL::Profile_branch_counter NAME(Y); ++NAME; 00162 # define CGAL_BRANCH_PROFILER_BRANCH(NAME) \ 00163 NAME.increment_branch(); 00164 # define CGAL_BRANCH_PROFILER_3(Y, NAME) \ 00165 static CGAL::Profile_branch_counter_3 NAME(Y); ++NAME; 00166 # define CGAL_BRANCH_PROFILER_BRANCH_1(NAME) \ 00167 NAME.increment_branch_1(); 00168 # define CGAL_BRANCH_PROFILER_BRANCH_2(NAME) \ 00169 NAME.increment_branch_2(); 00170 #else 00171 # define CGAL_PROFILER(Y) 00172 # define CGAL_HISTOGRAM_PROFILER(Y, Z) 00173 # define CGAL_BRANCH_PROFILER(Y, NAME) 00174 # define CGAL_BRANCH_PROFILER_BRANCH(NAME) 00175 # define CGAL_BRANCH_PROFILER_3(Y, NAME) 00176 # define CGAL_BRANCH_PROFILER_BRANCH_1(NAME) 00177 # define CGAL_BRANCH_PROFILER_BRANCH_2(NAME) 00178 #endif 00179 00180 CGAL_END_NAMESPACE 00181 00182 #endif // CGAL_PROFILE_COUNTER_H