module Utils::IRB::Shell
def irb_time_watch(duration = 1)
- Yield: - the block to be measured, receiving the iteration count as an argument
Parameters:
-
duration
(Integer
) -- the time interval in seconds between
def irb_time_watch(duration = 1) start = Time.now pre = nil avg = Hash.new i = 0 fetch_next = -> cur do pre = cur.map(&:to_f) i += 1 sleep duration end loop do cur = [ yield(i) ].flatten unless pre fetch_next.(cur) redo end expired = Time.now - start diffs = cur.zip(pre).map { |c, p| c - p } rates = diffs.map { |d| d / duration } durs = cur.zip(rates).each_with_index.map { |(c, r), i| if r < 0 x = c.to_f / -r a = avg[i].to_f a -= a / 2 a += x / 2 d = Tins::Duration.new(a) ds = d.to_s ds.singleton_class { define_method(:to_f) { d.to_f } } avg[i] = ds end avg[i] } warn "#{expired} #{cur.zip(diffs, rates, durs) * ' '} 𝝙 / per sec." fetch_next.(cur) sleep duration end end