Skip to content

Instantly share code, notes, and snippets.

@lmarlow
Created January 14, 2010 00:49
Show Gist options
  • Save lmarlow/276745 to your computer and use it in GitHub Desktop.
Save lmarlow/276745 to your computer and use it in GitHub Desktop.
# simple timing without blocks
@@last_tick, @@last_line = Time.now.to_f, 0
def tick(new_line)
last_tick, last_line, new_tick = @@last_tick, @@last_line, Time.now.to_f
if last_line > 0
puts "Line#{new_line - last_line > 2 ? %Q(s #{last_line + 1}-#{new_line - 1}) : %Q( #{new_line - 1})} took #{'%0.3f' % (new_tick - last_tick)}s"
else
puts "Line #{new_line} executing #{'%0.3f' % (new_tick - last_tick)}s after tick defined"
end
@@last_tick, @@last_line = new_tick, new_line
end
tick(__LINE__)
(1..42).each { |x| sleep(x/1000.0) }
tick(__LINE__)
x = 4
y = 3
sleep(0.02)
z = x * y
tick(__LINE__)
# Outputs:
# Line 15 executing 0.000s after tick defined
# Line 16 took 0.909s
# Lines 18-21 took 0.020s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment