Class: TinyTimer::Timer
- Inherits:
-
Object
- Object
- TinyTimer::Timer
- Defined in:
- lib/tiny_timer/timer.rb
Constant Summary collapse
- @@timers =
[]
- @@leading =
''
Instance Attribute Summary collapse
-
#real_running_time ⇒ Object
readonly
Returns the value of attribute real_running_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
-
#comment(desc = '') ⇒ Object
use this method if you only want to print out something, do not create an empty step or timer.
-
#initialize ⇒ Timer
constructor
A new instance of Timer.
-
#step(desc = '') ⇒ Object
if no block given, should do nothing.
Constructor Details
#initialize ⇒ Timer
Returns a new instance of Timer.
34 35 36 37 38 |
# 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 |
Instance Attribute Details
#real_running_time ⇒ Object (readonly)
Returns the value of attribute real_running_time.
32 33 34 |
# File 'lib/tiny_timer/timer.rb', line 32 def real_running_time @real_running_time end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
32 33 34 |
# File 'lib/tiny_timer/timer.rb', line 32 def start_time @start_time end |
Class Method Details
.count ⇒ Object
23 24 25 |
# File 'lib/tiny_timer/timer.rb', line 23 def count @@timers.size end |
.current_timer ⇒ Object
27 28 29 |
# File 'lib/tiny_timer/timer.rb', line 27 def current_timer @@timers.last end |
.run_timer(desc = nil, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#comment(desc = '') ⇒ Object
use this method if you only want to print out something, do not create an empty step or timer
60 61 62 |
# File 'lib/tiny_timer/timer.rb', line 60 def comment(desc='') puts "#{@@leading}#{STEP_CHARS}[ #{desc} ]" if desc.length > 0 end |
#step(desc = '') ⇒ Object
if no block given, should do nothing
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 |