Skip to content

Instantly share code, notes, and snippets.

@esaborit4code
Created August 9, 2018 10:33
Show Gist options
  • Save esaborit4code/cdd61ad21f21a780f1039d8048d273df to your computer and use it in GitHub Desktop.
Save esaborit4code/cdd61ad21f21a780f1039d8048d273df to your computer and use it in GitHub Desktop.
Cheap Benchmark
class BM
def self.bm(name)
@stats ||= {}
@stats[name] ||= { time: 0, times_called: 0, avg_time: 0 }
start_time = Time.zone.now
yield
@stats[name][:time] += (Time.zone.now - start_time)
@stats[name][:times_called] += 1
@stats[name][:avg_time] = @stats[name][:time].to_f / @stats[name][:times_called]
end
def self.stats
@stats
end
end
BM.bm 'code description' do
your_code
doing_things
end
p BM.stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment