DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions
DGtal::Clock Class Reference

#include <Clock.h>

Public Member Functions

void startClock ()
double stopClock ()
 Clock ()
 ~Clock ()
void selfDisplay (std::ostream &out) const
bool isValid () const

Detailed Description

Description of class 'Clock'

Aim: To provide functions to start and stop a timer. Is useful to get performance of algorithms.

The following code snippet demonstrates how to use Clock

#include <DGtal/base/Clock.h>
Clock c;
long duration;
c.startClock();
...
//do something
...
duration = c.stopClock();
std::cout<< "Duration in ms. : "<< duration <<endl;
See also:
testClock.cpp

Definition at line 95 of file Clock.h.


Constructor & Destructor Documentation

DGtal::Clock::Clock ( )
inline

Constructor.

Definition at line 55 of file Clock.ih.

{
}
DGtal::Clock::~Clock ( )
inline

Destructor.

Definition at line 130 of file Clock.ih.

{
}

Member Function Documentation

bool DGtal::Clock::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 156 of file Clock.ih.

{
return true;
}
void DGtal::Clock::selfDisplay ( std::ostream &  out) const
inline

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Definition at line 145 of file Clock.ih.

{
out << "[Clock]";
}
void DGtal::Clock::startClock ( )
inline

Starts a clock.

Definition at line 63 of file Clock.ih.

Referenced by DGtal::Trace::beginBlock().

{
#if ( (defined(WIN32)) )
myFirstTick = clock();
if (myFirstTick == (clock_t) -1)
{
cerr << "[Clock::startClock] Erreur sur 'clock()'." << endl;
}
#else
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
myTimerStart.tv_sec = mts.tv_sec;
myTimerStart.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &myTimerStart);
#endif
#endif
}
double DGtal::Clock::stopClock ( )
inline

Stops the clock.

Returns:
the time (in ms) since the last 'startClock()'.

Definition at line 91 of file Clock.ih.

Referenced by DGtal::Trace::endBlock().

{
#if ( (defined(WIN32)) )
clock_t last_tick = clock();
if (last_tick == (clock_t) -1)
{
cerr << "[Clock::stopClock] Erreur sur 'clock()'." << endl;
}
return (double) ((double) 1000.0 * (double)(last_tick - myFirstTick)
/ (double) CLOCKS_PER_SEC);
#else
struct timespec current;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
current.tv_sec = mts.tv_sec;
current.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &current); //Linux gettime
#endif
return (( current.tv_sec - myTimerStart.tv_sec) *1000 +
( current.tv_nsec - myTimerStart.tv_nsec)/1000000.0);
#endif
}

The documentation for this class was generated from the following files: