Skip to content

Instantly share code, notes, and snippets.

@jcmuller
Last active March 16, 2017 22:15
Show Gist options
  • Save jcmuller/a84eda68119983fc5aaf7a9838f63062 to your computer and use it in GitHub Desktop.
Save jcmuller/a84eda68119983fc5aaf7a9838f63062 to your computer and use it in GitHub Desktop.
Someone today asked for a way to aggregate timings of operations in different places across an app. This is a single threaded solution.
class Timer
include Singleton
def initialize
Thread.current[:timers] ||= Hash.new(0)
end
def display(timer)
Thread.current[:timers][timer]
end
def reset(timer)
Thread.current[:timers][timer] = 0
end
def time(timer, &block)
start = Time.zone.now
block.call
time_end = Time.zone.now
Thread.current[:timers][timer] += time_end - start
end
end
def time(timer, &block)
Timer.instance.time(timer, &block)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment