开发者

Ruby Time Calculations

I am trying to determine the duration of elapsed time minus any interruptions. The method below seems inefficient and silly is there a b开发者_如何学编程etter 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜