class TinyTimer::Timer

Attributes

real_running_time[R]
start_time[R]

Public Class Methods

count() click to toggle source
# File lib/tiny_timer/timer.rb, line 23
def count
  @@timers.size
end
current_timer() click to toggle source
# File lib/tiny_timer/timer.rb, line 27
def current_timer
  @@timers.last
end
new() click to toggle source
# File lib/tiny_timer/timer.rb, line 34
def initialize
  @start_time = Time.now
  @step_count = 0
  @real_running_time = 0 # sum of the running time of all steps
end
run_timer(desc=nil) { || ... } click to toggle source
# File lib/tiny_timer/timer.rb, line 11
def run_timer(desc=nil, &block)
  # desc ||= Rake.application.last_description
  new_timer = self.new
  @@timers.push(new_timer)
  @@leading += LEADING_CHARS if self.count > 1 # 4 whitespaces
  puts "#{@@leading}#{TIMER_CHARS}Started: #{desc}" #TODO: ANSI color for Started
  yield
  puts "#{@@leading}#{TIMER_CHARS}Finished. Total running time: #{Time.now - new_timer.start_time} seconds"
  @@leading.sub!(/.{#{LEADING_CHARS.size}}$/,'') if self.count > 1
  @@timers.pop
end

Public Instance Methods

comment(desc='') click to toggle source

use this method if you only want to print out something, do not create an empty step or timer

# File lib/tiny_timer/timer.rb, line 60
def comment(desc='')
  puts "#{@@leading}#{STEP_CHARS}[ #{desc} ]" if desc.length > 0
end
step(desc='') { || ... } click to toggle source

if no block given, should do nothing

# File lib/tiny_timer/timer.rb, line 41
def step(desc='')
  if block_given?
    if @block_flag
      yield
    else
      @step_count += 1
      puts "#{@@leading}#{STEP_CHARS}#{@step_count}. #{desc} ... "
      @block_flag = true
      start = Time.now
      yield
      running_time = Time.now - start
      @block_flag = false
      @real_running_time += running_time
      puts "#{@@leading}#{STEP_CHARS}-- finished in #{running_time} seconds"
    end
  end
end