Ruby: How to measure code execution time in an IRB or Rails console

Paste this method into your console:

def time(&block) puts Benchmark.measure(&block) end

Now time { Some.lengthy_task } will behave similar to the bash time command. Of course you can do much more with the Benchmark object than just putsing it – adapt to your needs.

Dominik Schöler Over 10 years ago