Hitimes are upon us

Saturday afternoon at the Lone Star Ruby Conf I was discussing something, I forget exactly what, with Bruce and he mentioned that he was looking for a really fast way to measure elapsed time of a piece of code.

The naive approach would be call Time.now before and after the call, or maybe use the Time.elapse method from Facets. Both of those approaches are perfectly acceptable, and return results down to the µsecond. Can we do better? Yes.

We have a goal of being faster than 2 successive Time.now calls. Each operating system has a different way accessing a high-resolution timer.

  • Unixes - the POSIX calls to clock_gettime()
  • OS X - UpTime()
  • Windows - QueryPerformanceCounter()

Unify the C level interface to all of those, wrap it up in a Ruby extension and the result is the just-released Hitimes, a high-resolution timer library in Ruby for when you want to do measuring at the nanosecond level.

gem install hitimes

The simplest and lightest weight way to use the library is via the Interval class. This does one really simple thing. It measures an interval of time. You can measure code in a block:

require 'hitimes'
require 'open-uri'
duration = Hitimes::Interval.measure do
            bruce = open("http://www.codefluency.com/").read
puts duration # => 0.261841618

Or measure a particular piece of code for performance:

interval = Hitimes::Interval.new
# do something
# some code to measure
duration = interval.stop

There are other approaches using Interval, and a Timer class for measuring series of intervals and reporting statistics.

Comments (View)

Post Lone Star Talk Thoughts

Fernand and I presented a talk at Lone Star Ruby Conf last Friday entitled Building and Managing a Ruby Infrastructure. It appears we hit upon a subject that people are starting encounter. Over the next couple of weeks I'll post a few more articles explaining in more detail a few of the key concepts. For now, a few links to things that we mentioned in the talk.

  • Bones -- A great project templating tool, extendible to use your own proect templates.
  • Rational Versioning Policy -- in which ~> a.k.a "Twiddle Wakka" is explained.
  • Stickler -- organize and maintain an internal gem distribution server. Still in planning stages.
  • take a look at the gem index and gem server commands

Comments (View)