views:

204

answers:

1

I am trying to determine the duration of elapsed time minus any interruptions. The method below seems inefficient and silly is there a better method?

The user specifies an end_time and a start_time are and records any interruptions as an integer representing minutes.

def duration
  ((end_time - start_time).seconds - interrupt.minutes) / 60
end
+2  A: 

Have you looked into the Benchmarks Module?

Seems to do exactly what you want and it's part of the Ruby core libraries.

require 'benchmark'

Benchmark.bm do |x|
  x.report block_to_time
end

It's output is very similar to the unix time command. Detailing time spent in kernel space, time spent in user space and total CPU time, and the real elapsed time.

EmFi
I should have clarified (and now have) this these values are from user inputs. Though the Benchmarks Module though seems really useful otherwise.
ahsteele