From: Robert Klemme Date: 2012-08-10T05:13:33+09:00 Subject: Re: Time-efficient program On Thu, Aug 9, 2012 at 5:52 PM, ajay paswan wrote: > Suppose you are given a program, now you are given a task to minimize > the time, what will be your approach? What time? > and how will you measure the time needed? > I think if we can get total clock cycle by any mean, that could be the > best measure. If so, how can I get that? If you want to measure execution time of a Ruby program there's Benchmark. irb19robert@fussel:~$ irb19 -r benchmark irb(main):001:0> Benchmark.measure { sleep 1 } => 0.000000 0.000000 0.000000 ( 1.000137) irb(main):002:0> Benchmark.measure { 10_000.times { |i| 10 + i } } => 0.000000 0.000000 0.000000 ( 0.001673) irb(main):003:0> Benchmark.measure { 1000_000.times { |i| 10 + i } } => 0.170000 0.000000 0.170000 ( 0.170213) irb(main):004:0> Benchmark.bm {|x| x.report("foo") { sleep 1 }} user system total real foo 0.000000 0.000000 0.000000 ( 1.000152) => [ 0.000000 0.000000 0.000000 ( 1.000152) ] http://rubydoc.info/stdlib/benchmark/frames Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/