Re-implement to be more threads-friendly (a first step towards thread-safety).

This commit is contained in:
Sylvain Pion 2008-12-04 01:57:33 +00:00
parent 68c1bcc095
commit aa06636838
2 changed files with 21 additions and 19 deletions

View File

@ -2,14 +2,17 @@
#include <CGAL/Profile_timer.h> #include <CGAL/Profile_timer.h>
int main() void f()
{
CGAL_TIME_PROFILER("seconds spent in this for loop");
for (int i=0; i<10; ++i)
{ {
CGAL_TIME_PROFILER("seconds spent in this function (total time)");
// do something // do something
double d = 1+1; double d = 1+1;
(void) d; (void) d;
} }
int main()
{
for (int i=0; i<100000; ++i)
f();
return 0; return 0;
} }

View File

@ -23,8 +23,9 @@
// This file contains a class to help in profiling, together with macros // This file contains a class to help in profiling, together with macros
// triggered by CGAL_PROFILE to enable it: // triggered by CGAL_PROFILE to enable it:
// //
// - Profile_timer which keeps track of the time spent in its scope-block. // - Profile_timer which keeps track of the time spent in its scope-block,
// (be careful at recursive functions :-). // it accumulates the timings if it goes there several times.
// Note: be careful at recursive functions...
// //
// See also CGAL/Profile_counter.h // See also CGAL/Profile_counter.h
@ -39,26 +40,24 @@ CGAL_BEGIN_NAMESPACE
struct Profile_timer struct Profile_timer
{ {
class Local { class Local {
Profile_timer &p; Real_timer rt;
Profile_timer *p;
public: public:
Local(Profile_timer& p_) : p(p_) { p.start(); } Local(Profile_timer* p_) : p(p_) { rt.start(); }
~Local() { p.stop(); } ~Local() { rt.stop(); p->t += rt.time(); }
}; };
Profile_timer(const std::string & ss) Profile_timer(const std::string & ss)
: s(ss) { t.reset(); } : t(0) {}
void start() { t.start(); }
void stop() { t.stop(); }
~Profile_timer() ~Profile_timer()
{ {
std::cerr << "[CGAL::Profile_timer] " std::cerr << "[CGAL::Profile_timer] "
<< std::setw(10) << t.time() << " " << s << std::endl; << std::setw(10) << t << " " << s << std::endl;
} }
private: private:
Real_timer t; double t;
const std::string s; const std::string s;
}; };
@ -66,7 +65,7 @@ private:
#ifdef CGAL_PROFILE #ifdef CGAL_PROFILE
# define CGAL_TIME_PROFILER(NAME) \ # define CGAL_TIME_PROFILER(NAME) \
static CGAL::Profile_timer CGAL_profile_timer_tmp(NAME); \ static CGAL::Profile_timer CGAL_profile_timer_tmp(NAME); \
CGAL::Profile_timer::Local CGAL_local_profile_timer_tmp(CGAL_profile_timer_tmp); CGAL::Profile_timer::Local CGAL_local_profile_timer_tmp(&CGAL_profile_timer_tmp);
#else #else
# define CGAL_TIME_PROFILER(NAME) # define CGAL_TIME_PROFILER(NAME)
#endif #endif