Turi Create  4.0
turi::timer Class Reference

A simple class that can be used for benchmarking/timing up to microsecond resolution. More...

#include <core/system/platform/timer/timer.hpp>

Public Member Functions

 timer ()
 The timer starts on construction but can be restarted by calling turi::timer::start.
 
void start ()
 Reset the timer.
 
double current_time () const
 Returns the elapsed time in seconds since turi::timer::start was last called. More...
 
double current_time_millis () const
 Returns the elapsed time in milliseconds since turi::timer::start was last called. More...
 

Static Public Member Functions

static double sec_of_day ()
 Get the number of seconds (as a floating point value) since the Unix Epoch.
 
static size_t usec_of_day ()
 Returns only the micro-second component of the time since the Unix Epoch.
 
static float approx_time_seconds ()
 Returns the time since program start. More...
 
static size_t approx_time_millis ()
 Returns the time since program start. More...
 
static void stop_approx_timer ()
 Stops the approximate timer. More...
 
static void sleep (size_t sleeplen)
 
static void sleep_ms (size_t sleeplen)
 

Detailed Description

A simple class that can be used for benchmarking/timing up to microsecond resolution.

Standard Usage

The timer is used by calling turi::timer::start and then by getting the current time since start by calling turi::timer::current_time.

For example:

#include <timer/timer.hpp>
timer.start();
// do something
std::cout << "Elapsed time: " << timer.current_time() << std::endl;

Sleeping

The sleep routines here are preferred than the sleep method provided by the standard C library. On Linux, the C sleep method can be woken up by signals, hence it may not sleep the full prescribed interval. The sleep implemented in the timer are more resilient.

Fast approximate time

Calling current item in a tight loop can be costly and so we provide a faster less accurate timing primitive which reads a local time variable that is updated roughly every 100 millisecond. These are the turi::timer::approx_time_seconds and turi::timer::approx_time_millis.

Definition at line 59 of file timer.hpp.

Member Function Documentation

◆ approx_time_millis()

static size_t turi::timer::approx_time_millis ( )
static

Returns the time since program start.

This value is only updated once every 100ms and is therefore approximate (but fast).

◆ approx_time_seconds()

static float turi::timer::approx_time_seconds ( )
static

Returns the time since program start.

This value is only updated once every 100ms and is therefore approximate (but fast).

◆ current_time()

double turi::timer::current_time ( ) const
inline

Returns the elapsed time in seconds since turi::timer::start was last called.

Returns
time in seconds since turi::timer::start was called.

Definition at line 83 of file timer.hpp.

◆ current_time_millis()

double turi::timer::current_time_millis ( ) const
inline

Returns the elapsed time in milliseconds since turi::timer::start was last called.

Returns
time in milliseconds since turi::timer::start was called.

Definition at line 100 of file timer.hpp.

◆ sleep()

static void turi::timer::sleep ( size_t  sleeplen)
static

Sleeps for sleeplen seconds

◆ sleep_ms()

static void turi::timer::sleep_ms ( size_t  sleeplen)
static

Sleeps for sleeplen milliseconds.

◆ stop_approx_timer()

static void turi::timer::stop_approx_timer ( )
static

Stops the approximate timer.

This stops the approximate timer thread. Once stoped, the approximate time will never be advanced again. This function should not generally be used, but it seems like on certain platforms (windows for instance) it does not like terminating threads inside DLLs at program terminiation. This can be used to force thread termination.

See also
approx_time_seconds approx_time_millis

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