Class: Hitimes::Interval

Inherits:
Object
  • Object
show all
Defined in:
ext/hitimes/c/hitimes_interval.c,
ext/hitimes/c/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

.measure { ... } ⇒ Float

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

Yields:

Returns:

  • (Float)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/hitimes/c/hitimes_interval.c', line 62

VALUE hitimes_interval_measure( )
{
    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" );
    }

    before = hitimes_get_current_instant( );
    rb_yield( Qnil );
    after  = hitimes_get_current_instant( );

    duration = ( after - before ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    return rb_float_new( duration );
}

.nowInterval

Create an interval that has already started

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/hitimes/c/hitimes_interval.c', line 42

VALUE hitimes_interval_now( )
{
    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

#durationFloat #to_fFloat #to_secondsFloat #lengthFloat

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.

Raises Error if duration is called on an interval that has not started yet.

Overloads:

  • #durationFloat

    Returns:

    • (Float)
  • #to_fFloat

    Returns:

    • (Float)
  • #to_secondsFloat

    Returns:

    • (Float)
  • #lengthFloat

    Returns:

    • (Float)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/hitimes/c/hitimes_interval.c', line 287

VALUE hitimes_interval_duration ( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    /* raise an error if the internval is not started */
    if ( 0L == i->start_instant )  {
        rb_raise(eH_Error, "Attempt to report a duration on an interval that has not started" );
    }


    /**
     * 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 );
    }

    /*
     * stop has been called, calculate the duration and save the result
     */
    if ( i->duration < 0.0 ) {
        i->duration = ( i->stop_instant - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    }

    return rb_float_new( i->duration );
}

#duration_so_farFloat, 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.

Returns:

  • (Float, false)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/hitimes/c/hitimes_interval.c', line 167

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;
    }

    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;
        rc = rb_float_new( d );
    }
    return rc;
}

#durationFloat #to_fFloat #to_secondsFloat #lengthFloat

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.

Raises Error if duration is called on an interval that has not started yet.

Overloads:

  • #durationFloat

    Returns:

    • (Float)
  • #to_fFloat

    Returns:

    • (Float)
  • #to_secondsFloat

    Returns:

    • (Float)
  • #lengthFloat

    Returns:

    • (Float)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/hitimes/c/hitimes_interval.c', line 287

VALUE hitimes_interval_duration ( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    /* raise an error if the internval is not started */
    if ( 0L == i->start_instant )  {
        rb_raise(eH_Error, "Attempt to report a duration on an interval that has not started" );
    }


    /**
     * 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 );
    }

    /*
     * stop has been called, calculate the duration and save the result
     */
    if ( i->duration < 0.0 ) {
        i->duration = ( i->stop_instant - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    }

    return rb_float_new( i->duration );
}

#running?Boolean

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

Returns:

  • (Boolean)


225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/hitimes/c/hitimes_interval.c', line 225

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;
    }

    return rc;
}

#splitInterval

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

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/hitimes/c/hitimes_interval.c', line 87

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;
}

#startBoolean

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.

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/hitimes/c/hitimes_interval.c', line 114

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;
    }

    return rc;
}

#start_instantInteger

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

Returns:

  • (Integer)


246
247
248
249
250
251
252
253
# File 'ext/hitimes/c/hitimes_interval.c', line 246

VALUE hitimes_interval_start_instant( VALUE self )
{
    hitimes_interval_t *i;

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

#started?Boolean

returns whether or not the interval has been started

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
# File 'ext/hitimes/c/hitimes_interval.c', line 193

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;
}

#stopBoolean, 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.

Returns:

  • (Boolean, Float)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/hitimes/c/hitimes_interval.c', line 140

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" );
    }

    if ( 0L == i->stop_instant ) {
      i->stop_instant = hitimes_get_current_instant( );
      i->duration = ( i->stop_instant - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
      rc = rb_float_new( i->duration );
    }

    return rc;
}

#stop_instantInteger

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

Returns:

  • (Integer)


263
264
265
266
267
268
269
270
# File 'ext/hitimes/c/hitimes_interval.c', line 263

VALUE hitimes_interval_stop_instant( VALUE self )
{
    hitimes_interval_t *i;

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

#stopped?Boolean

returns whether or not the interval has been stopped

Returns:

  • (Boolean)


209
210
211
212
213
214
215
216
# File 'ext/hitimes/c/hitimes_interval.c', line 209

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;
}

#durationFloat #to_fFloat #to_secondsFloat #lengthFloat

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.

Raises Error if duration is called on an interval that has not started yet.

Overloads:

  • #durationFloat

    Returns:

    • (Float)
  • #to_fFloat

    Returns:

    • (Float)
  • #to_secondsFloat

    Returns:

    • (Float)
  • #lengthFloat

    Returns:

    • (Float)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/hitimes/c/hitimes_interval.c', line 287

VALUE hitimes_interval_duration ( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    /* raise an error if the internval is not started */
    if ( 0L == i->start_instant )  {
        rb_raise(eH_Error, "Attempt to report a duration on an interval that has not started" );
    }


    /**
     * 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 );
    }

    /*
     * stop has been called, calculate the duration and save the result
     */
    if ( i->duration < 0.0 ) {
        i->duration = ( i->stop_instant - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    }

    return rb_float_new( i->duration );
}

#durationFloat #to_fFloat #to_secondsFloat #lengthFloat

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.

Raises Error if duration is called on an interval that has not started yet.

Overloads:

  • #durationFloat

    Returns:

    • (Float)
  • #to_fFloat

    Returns:

    • (Float)
  • #to_secondsFloat

    Returns:

    • (Float)
  • #lengthFloat

    Returns:

    • (Float)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/hitimes/c/hitimes_interval.c', line 287

VALUE hitimes_interval_duration ( VALUE self )
{
    hitimes_interval_t *i;

    Data_Get_Struct( self, hitimes_interval_t, i );

    /* raise an error if the internval is not started */
    if ( 0L == i->start_instant )  {
        rb_raise(eH_Error, "Attempt to report a duration on an interval that has not started" );
    }


    /**
     * 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 );
    }

    /*
     * stop has been called, calculate the duration and save the result
     */
    if ( i->duration < 0.0 ) {
        i->duration = ( i->stop_instant - i->start_instant ) / HITIMES_INSTANT_CONVERSION_FACTOR;
    }

    return rb_float_new( i->duration );
}