Created
August 9, 2018 10:33
-
-
Save esaborit4code/cdd61ad21f21a780f1039d8048d273df to your computer and use it in GitHub Desktop.
Cheap Benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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