Tic Toc And IRB

Posted on 01 Feb 2013 by Eric Oestrich

My co-worker, Sam Goldman, a while back showed me this useful little IRB trick in order to get out how long a method takes. I used it pretty heavily recently with doing a call that hits the network.

It’s really easy to set up, just add the following to your .irbrc file.

~/.irbrc
def tic
  @tic = Time.now
end

def toc
  "#{Time.now - @tic} seconds"
end

Once that’s set up you can call it surrouding the method you want to time.

irb(main):003:0> tic; puts "Hello"; toc
Hello
#=> "3.0686e-05 seconds"
irb(main):004:0> tic; sleep 2; toc
#=> "2.000082375 seconds"

It’s also in my dotfiles repo on github.

comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.