Class: BenchmarkInterface::Benchmark
- Inherits:
-
Object
- Object
- BenchmarkInterface::Benchmark
- Defined in:
- lib/benchmark-interface/benchmark.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #basic_iteration_time ⇒ Object
-
#initialize(name, block) ⇒ Benchmark
constructor
A new instance of Benchmark.
- #iterate(iterations) ⇒ Object
- #iterations_for_one_second ⇒ Object
- #needs_iterating? ⇒ Boolean
- #remove_line_numbers ⇒ Object
- #time_block(desired_time) ⇒ Object
Constructor Details
#initialize(name, block) ⇒ Benchmark
Returns a new instance of Benchmark.
14 15 16 17 |
# File 'lib/benchmark-interface/benchmark.rb', line 14 def initialize(name, block) @name = name @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
12 13 14 |
# File 'lib/benchmark-interface/benchmark.rb', line 12 def block @block end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/benchmark-interface/benchmark.rb', line 12 def name @name end |
Instance Method Details
#basic_iteration_time ⇒ Object
41 42 43 44 |
# File 'lib/benchmark-interface/benchmark.rb', line 41 def basic_iteration_time time, iterations = time_block(0.1) time / iterations.to_f end |
#iterate(iterations) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/benchmark-interface/benchmark.rb', line 55 def iterate(iterations) original_block = @block if original_block.arity == 1 @block = Proc.new do original_block.call iterations end else @block = Proc.new do iterations.times do original_block.call end end end end |
#iterations_for_one_second ⇒ Object
46 47 48 49 |
# File 'lib/benchmark-interface/benchmark.rb', line 46 def iterations_for_one_second _, iterations = time_block(1) iterations end |
#needs_iterating? ⇒ Boolean
51 52 53 |
# File 'lib/benchmark-interface/benchmark.rb', line 51 def @block.arity == 1 end |
#remove_line_numbers ⇒ Object
19 20 21 |
# File 'lib/benchmark-interface/benchmark.rb', line 19 def remove_line_numbers @name = @name.split(':')[0...-1].join(':') if @name.include? ':' end |
#time_block(desired_time) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/benchmark-interface/benchmark.rb', line 23 def time_block(desired_time) iterations = 1 while true start = Time.now if block.arity == 1 block.call iterations else iterations.times do block.call end end time = Time.now - start return [time, iterations] if time >= desired_time iterations *= 2 end end |