On Thu, Aug 9, 2012 at 5:52 PM, ajay paswan <[email protected]> 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/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to