synaps/base/Clock.h

00001 /********************************************************************
00002  *   This file is part of the source code of the SYNAPS kernel.
00003  *   Author(s): B. Mourrain, GALAAD, INRIA
00004  *   $Id: Clock.h,v 1.1 2005/07/11 11:23:11 mourrain Exp $
00005  ********************************************************************/
00006 #ifndef SYNAPS_UTIL_CLOCK_H
00007 #define SYNAPS_UTIL_CLOCK_H
00008 /********************************************************************/
00009 
00010 
00011 #include <ctime>
00012 #include <synaps/init.h>
00013 
00014 __BEGIN_NAMESPACE_SYNAPS
00015 
00016 class Clock
00017 {
00018         public:
00019 
00020                 // Construct and start clock immediately.
00021                 //
00022                 Clock() : start_(clock()), running(true) {};
00023 
00024                 ~Clock() {};
00025 
00026                 // Start clock; same effect as constructor.
00027                 //
00028                 void start()
00029                 {
00030                         start_ = clock();
00031                         running = true;
00032                 }
00033 
00034                 // Time returned by time() is from start of clock
00035                 // until the call of this function.
00036                 //
00037                 void stop()
00038                 {
00039                         stop_ = clock();
00040                         running = false;
00041                 }
00042 
00043                 // Return elapsed CPU time from start of clock until the call
00044                 // of stop(), or the current value if clock is running.
00045                 //
00046                 double time()
00047                 {
00048                         if (running)
00049                                 return double(clock() - start_) / CLOCKS_PER_SEC;
00050                         else
00051                                 return double(stop_ - start_) / CLOCKS_PER_SEC;
00052                 }
00053 
00054         private:
00055                 clock_t start_;
00056                 clock_t stop_;
00057                 bool running;
00058 };
00059 
00060 __END_NAMESPACE_SYNAPS
00061 
00062 #endif  // SYNAPS_UTIL_CLOCK_H
00063 

SYNAPS DOCUMENTATION
logo