Class: Hitimes::Interval

Inherits:
Object
  • Object
show all
Defined in:
ext/hitimes/hitimes_interval.c

Overview

This is the lowest level timing mechanism available. It allows for easy measuring based upon a block:

duration = Interval.measure { ... }

Or measuring something specifically

interval = Interval.new
interval.start
duration = interval.stop

Allocating and starting an interval can be done in one method call with

interval = Interval.now

Interval is useful when you only need to track a single interval of time, or if you do not want to track statistics about an operation.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) measure

*

call-seq:
   Interval.measure {  }  -> Float

Times the execution of the block returning the number of seconds it took


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_measure( VALUE self )
{
hitimes_instant_t before;
hitimes_instant_t after;
long double       duration;

if ( !rb_block_given_p() ) {
    rb_raise(eH_Error, "No block given to Interval.measure" );
}

+ (Object) now

*

call-seq:
   Interval.now -> Interval

Create an interval that has already started


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_now( VALUE self )
{
    VALUE obj;
    hitimes_interval_t *i = xmalloc( sizeof( hitimes_interval_t ) );

    i->start_instant = hitimes_get_current_instant( );
    i->stop_instant  = 0L;
    i->duration      = -1.0l;

    obj = Data_Wrap_Struct(cH_Interval, NULL, hitimes_interval_free, i);

    return obj;
}

Instance Method Details

- (Object) duration

*

call-seq:
   interval.duration -> Float
   interval.to_f -> Float
   interval.to_seconds -> Float
   interval.length -> Float

Returns the Float value of the interval, the value is in seconds.  If the
interval has not had stop called yet, it will report the number of seconds
in the interval up to the current point in time.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_duration ( VALUE self )
{
hitimes_interval_t *i;

Data_Get_Struct( self, hitimes_interval_t, i );

/** 
 * if stop has not yet been called, then return the amount of time so far
 */
if ( 0L == i->stop_instant ) {
    long double d;
    hitimes_instant_t now = hitimes_get_current_instant( );
    d = ( now - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    return rb_float_new( d );
}

- (Object) duration_so_far

*

call-seq:
    interval.duration_so_far -> Float or false

return how the duration so far.  This will return the duration from the time
the Interval was started if the interval is running, otherwise it will return
false.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_duration_so_far( VALUE self )
{
hitimes_interval_t *i;
VALUE               rc = Qfalse;

Data_Get_Struct( self, hitimes_interval_t, i );
if ( 0L == i->start_instant ) {
    return rc;
}

- (Object) length

*

call-seq:
   interval.duration -> Float
   interval.to_f -> Float
   interval.to_seconds -> Float
   interval.length -> Float

Returns the Float value of the interval, the value is in seconds.  If the
interval has not had stop called yet, it will report the number of seconds
in the interval up to the current point in time.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_duration ( VALUE self )
{
hitimes_interval_t *i;

Data_Get_Struct( self, hitimes_interval_t, i );

/** 
 * if stop has not yet been called, then return the amount of time so far
 */
if ( 0L == i->stop_instant ) {
    long double d;
    hitimes_instant_t now = hitimes_get_current_instant( );
    d = ( now - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    return rb_float_new( d );
}

- (Object) running?

*

call-seq:
   interval.running? -> boolean

returns whether or not the interval is running or not.  This means that it
has started, but not stopped.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_running( VALUE self )
{
hitimes_interval_t *i;
VALUE              rc = Qfalse;

Data_Get_Struct( self, hitimes_interval_t, i );
if ( ( 0L != i->start_instant ) && ( 0L == i->stop_instant ) ) {
    rc = Qtrue;
}

- (Object) split

*

call-seq:
   interval.split -> Interval

Immediately stop the current interval and start a new interval that has a
start_instant equivalent to the stop_interval of self.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_split( VALUE self )
{
    hitimes_interval_t *first;
    hitimes_interval_t *second = xmalloc( sizeof( hitimes_interval_t ) );
    VALUE              obj;

    Data_Get_Struct( self, hitimes_interval_t, first );
    first->stop_instant = hitimes_get_current_instant( );

    second->start_instant = first->stop_instant;
    second->stop_instant  = 0L;
    second->duration      = -1.0l;

    obj = Data_Wrap_Struct(cH_Interval, NULL, hitimes_interval_free, second);

    return obj;
}

- (Object) start

*

call-seq:
   interval.start -> boolean

mark the start of the interval.  Calling start on an already started
interval has no effect.  An interval can only be started once.  If the
interval is truely started +true+ is returned otherwise +false+.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_start( VALUE self )
{
hitimes_interval_t *i;
VALUE               rc = Qfalse;

Data_Get_Struct( self, hitimes_interval_t, i );
if ( 0L == i->start_instant ) {
  i->start_instant = hitimes_get_current_instant( );
  i->stop_instant  = 0L;
  i->duration      = -1.0l;

  rc = Qtrue;
}

- (Object) start_instant

*

call-seq:
   interval.start_instant -> Integer

The integer representing the start instant of the Interval.  This value
is not useful on its own.  It is a platform dependent value.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_start_instant( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );
   
    return ULL2NUM( i->start_instant );
}

- (Object) started?

*

call-seq:
   interval.started? -> boolean

returns whether or not the interval has been started


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_started( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    return ( 0L == i->start_instant ) ? Qfalse : Qtrue;
}

- (Object) stop

*

call-seq:
   interval.stop -> bool or Float

mark the stop of the interval.  Calling stop on an already stopped interval
has no effect.  An interval can only be stopped once.  If the interval is
truely stopped then the duration is returned, otherwise +false+.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_stop( VALUE self )
{
hitimes_interval_t *i;
VALUE               rc = Qfalse;

Data_Get_Struct( self, hitimes_interval_t, i );
if ( 0L == i->start_instant )  {
    rb_raise(eH_Error, "Attempt to stop an interval that has not started.\n" );
}

- (Object) stop_instant

*

call-seq:
   interval.stop_instant -> Integer

The integer representing the stop instant of the Interval.  This value
is not useful on its own.  It is a platform dependent value.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_stop_instant( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );
   
    return ULL2NUM( i->stop_instant );
}

- (Object) stopped?

*

call-seq:
   interval.stopped? -> boolean

returns whether or not the interval has been stopped


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_stopped( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    return ( 0L == i->stop_instant ) ? Qfalse : Qtrue;
}

- (Object) to_f

*

call-seq:
   interval.duration -> Float
   interval.to_f -> Float
   interval.to_seconds -> Float
   interval.length -> Float

Returns the Float value of the interval, the value is in seconds.  If the
interval has not had stop called yet, it will report the number of seconds
in the interval up to the current point in time.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_duration ( VALUE self )
{
hitimes_interval_t *i;

Data_Get_Struct( self, hitimes_interval_t, i );

/** 
 * if stop has not yet been called, then return the amount of time so far
 */
if ( 0L == i->stop_instant ) {
    long double d;
    hitimes_instant_t now = hitimes_get_current_instant( );
    d = ( now - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    return rb_float_new( d );
}

- (Object) to_seconds

*

call-seq:
   interval.duration -> Float
   interval.to_f -> Float
   interval.to_seconds -> Float
   interval.length -> Float

Returns the Float value of the interval, the value is in seconds.  If the
interval has not had stop called yet, it will report the number of seconds
in the interval up to the current point in time.


# File 'ext/hitimes/hitimes_interval.c'

VALUE hitimes_interval_duration ( VALUE self )
{
hitimes_interval_t *i;

Data_Get_Struct( self, hitimes_interval_t, i );

/** 
 * if stop has not yet been called, then return the amount of time so far
 */
if ( 0L == i->stop_instant ) {
    long double d;
    hitimes_instant_t now = hitimes_get_current_instant( );
    d = ( now - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    return rb_float_new( d );
}