Class: BigO::TimeComplexity

Inherits:
Object
  • Object
show all
Includes:
ComplexityBase
Defined in:
lib/big-o/time_complexity.rb

Overview

Measure time complexity.

Instance Attribute Summary

Attributes included from ComplexityBase

#options, #result, #result_set, #scale

Instance Method Summary collapse

Methods included from ComplexityBase

#examine_result_set, #get_scale, #run_simulation

Constructor Details

#initialize(options = {}) ⇒ TimeComplexity

Raises the error percentage due to possible concurrency issue on the system (which in certain cases may cause some measures to be far longer than others).



8
9
10
11
# File 'lib/big-o/time_complexity.rb', line 8

def initialize(options = {})
  options = { :error_pct => 0.1 }.merge(options)
  super(options)
end

Instance Method Details

#measure(*args) { ... } ⇒ Float

Measures the execution time that fn is using.

Parameters:

  • args (Array)

    arguments which the given block should take

Yields:

  • function which should be measured (fn)

Returns:

  • (Float)

    measurement



25
26
27
28
29
30
# File 'lib/big-o/time_complexity.rb', line 25

def measure(*args, &b)
  t0 = Process.times
  b.call(*args)
  t1 = Process.times
  t1.utime - t0.utime
end

#processObject

Checks if the function can be measured and throw an error if it could not.



14
15
16
17
18
# File 'lib/big-o/time_complexity.rb', line 14

def process
  @scale ||= get_scale
  raise InstantaneousExecutionError.new unless @scale > 0
  super
end