Class: Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_with_extension_example/timer.rb

Overview

require ‘/Users/sean/dev/gems/gem_with_extension_example/lib/gem_with_extension_example/util.rb’

Constant Summary collapse

DIGITS_OF_PRESCISION =

ToDo Mode Flags:

:side_by_side (on by default)
:consecutive_runs (using redis)(off by default)
6
@@max_name_length =
0
@@max_whole_digits =
1
@@timer_list =
[]
@@mode =
:side_by_side

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Timer

private :digits, :update_digit_count



21
22
23
24
25
26
# File 'lib/gem_with_extension_example/timer.rb', line 21

def initialize(name)
  @name = name
  clear
  @@max_name_length = name.length if name.length > @@max_name_length
  @@timer_list << self
end

Instance Attribute Details

#elapsedObject (readonly)

Returns the value of attribute elapsed.



17
18
19
# File 'lib/gem_with_extension_example/timer.rb', line 17

def elapsed
  @elapsed
end

Class Method Details

.clearObject



87
88
89
90
91
# File 'lib/gem_with_extension_example/timer.rb', line 87

def self.clear
  @@max_name_length = 0
  @@max_whole_digits = 1
  @@timer_list = []
end

.digits(n) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/gem_with_extension_example/timer.rb', line 71

def digits(n)
  if n > 9
    d = Math.log10(n).to_i + 1
  elsif n < -9
    d = Math.log10(n * -1).to_i + 1
  else
    d = 1
  end
  d
end

.set_mode(mode) ⇒ Object



28
29
30
# File 'lib/gem_with_extension_example/timer.rb', line 28

def set_mode(mode)
  @@mode = mode #:side_by_side, :simple
end

.to_sObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gem_with_extension_example/timer.rb', line 31

def to_s
  # Process pair comparison
  pair_comparison = @@timer_list.count == 2
  speed_multiple = nil
  greater_index = nil
  if pair_comparison
    if @@timer_list[0].elapsed > @@timer_list[1].elapsed
      greater_index = 1
      speed_multiple = @@timer_list[0].elapsed / @@timer_list[1].elapsed
    else
      greater_index = 0
      speed_multiple = @@timer_list[1].elapsed / @@timer_list[0].elapsed
    end
  end
  total_time = @@timer_list.inject(0.0) { |result, t| result + t.elapsed }

  # construct output string
  s = ""
  @@timer_list.each_with_index do |t, i|
    s << t.to_s
    s << "%8.3f%s" % [((t.elapsed) / total_time * 100.0), '%'] if @@timer_list.count > 1
    if pair_comparison and (@@mode == :side_by_side) and (i == greater_index)
      s << ", %.1f times faster." % speed_multiple
    end
    s << "\n"
  end
  if !(@@mode == :side_by_side and pair_comparison) and @@timer_list.count > 1
    total_whole_digits = digits(total_time.to_i)
    digit_overflow = total_whole_digits - @@max_whole_digits
    leading_spaces = (@@max_name_length + 2) - digit_overflow

    s << (" " * (@@max_name_length + 2))
    s << ("-" * (DIGITS_OF_PRESCISION + @@max_whole_digits + 2))
    s << "\n"
    s << (" " * leading_spaces) if leading_spaces > 0
    s << "%#{DIGITS_OF_PRESCISION + total_whole_digits + 1}.#{DIGITS_OF_PRESCISION}fs" % total_time
    s << "\n"
  end
  s
end

Instance Method Details

#+(o) ⇒ Object



102
103
104
105
106
# File 'lib/gem_with_extension_example/timer.rb', line 102

def +(o)
  o = o.elapsed if o.is_a?(self.class)
  @elapsed += o
  update_digit_count
end

#/(o) ⇒ Object



107
108
109
110
111
# File 'lib/gem_with_extension_example/timer.rb', line 107

def /(o)
  o = o.elapsed if o.is_a?(self.class)
  @elapsed /= o
  update_digit_count
end

#clearObject



82
83
84
85
86
# File 'lib/gem_with_extension_example/timer.rb', line 82

def clear
  @state = :stopped
  @time = []
  @elapsed = 0.0
end

#digits(n) ⇒ Object



92
93
94
# File 'lib/gem_with_extension_example/timer.rb', line 92

def digits(n)
  self.class.digits(n)
end

#startObject



112
113
114
115
116
117
# File 'lib/gem_with_extension_example/timer.rb', line 112

def start
  if @state == :stopped
    @state = :running
    @start = Time.now
  end
end

#stopObject



118
119
120
121
122
123
124
125
# File 'lib/gem_with_extension_example/timer.rb', line 118

def stop
  if @state == :running
    stop = Time.now
    @elapsed += (stop - @start)
    update_digit_count
    @state = :stopped
  end
end

#to_sObject



99
100
101
# File 'lib/gem_with_extension_example/timer.rb', line 99

def to_s
   "% #{@@max_name_length}s: %#{DIGITS_OF_PRESCISION + @@max_whole_digits + 1}.#{DIGITS_OF_PRESCISION}fs" % [@name, @elapsed]
end

#update_digit_countObject



95
96
97
98
# File 'lib/gem_with_extension_example/timer.rb', line 95

def update_digit_count
  whole_digits = digits(@elapsed.to_i)
  @@max_whole_digits = whole_digits if whole_digits > @@max_whole_digits
end