Project

stoppur

0.0
No commit activity in last 3 years
No release in over 3 years
Simple time measurement, with monotonic clocks
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.1
~> 0.9
~> 0.7
 Project Readme

Stoppur

Build Status

Stoppur means stopwatch in Swedish, and is an attempt of avoiding using Time when measuring time in Ruby. The main problem with measuring time using Ruby's Time is that it is based on the system clock and thus isn't monotonic, which means that time can travel back and forth if the system's user decides to change the time, when toggling DST, or when adjusting for clock drift, etc.

Instead of Time, Stoppur uses one of the following mechanisms, depending on your system:

  • Ruby's Process.clock_gettime, if it is defined (Ruby 2.1+)
  • Java's System.nanoTime(), if using JRuby
  • clock_get_time() through a C extension, if available (OS X)
  • clock_gettime() through a C extension, if available (Linux)

Installation

$ gem install stoppur

Usage

require 'stoppur'

# measure simple block
elapsed = Stoppur.measure do
  # perform work here
end

# when you need more control
stopwatch = Stoppur.start
begin
  # might raise error
rescue => e
  puts 'raised error after %.4f s' % stopwatch.elapsed
end

# or when you want to measure more than one thing using the same stopwatch
stopwatch = Stoppur.start
# do some work
puts 'did some work in %.4f s' % stopwatch.elapsed
# do the rest of the work
puts 'did all of the work in %.4f s' % stopwatch.elapsed

Supported platforms

This should work for everyone using Ruby 2.1+ or JRuby, or for those running on *nix (including OS X).