This is pretty ugly:
t = Time.now
result = do_something
elapsed = Time.now - t
I tried this:
elapsed = time do
result = do_something
end
def time
t = Time.now
yield
Time.now - t
end
This is better. But the problem is that result
falls out of scope after the block ends.
So, is there a better way of doing timing? Or a good way to use the result
?
A really idiomatic way would be to use the standard library. :)
require 'benchmark'
result = nil
elapsed = Benchmark.realtime do
result = do_something
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With