Last active
March 12, 2020 11:49
-
-
Save TeWu/c5e47eda22c6c7cd6ee63d818f941d99 to your computer and use it in GitHub Desktop.
Benchmark finding max value
This file contains 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
require 'benchmark/ips' | |
def fast(a, b) | |
a > b ? a : b | |
end | |
def slow(a, b) | |
[a, b].max | |
end | |
class GCSuite | |
def warming(*) | |
run_gc | |
end | |
def running(*) | |
run_gc | |
end | |
def warmup_stats(*); end | |
def add_report(*); end | |
private | |
def run_gc | |
GC.enable | |
GC.start | |
GC.disable | |
end | |
end | |
Benchmark.ips do |x| | |
x.config(time: 10, warmup: 2, suite: GCSuite.new) | |
x.stats = :bootstrap | |
x.confidence = 95 | |
x.report('fast code') do |times| | |
i = 0 | |
while i < times | |
fast(100, 200) | |
i += 1 | |
end | |
end | |
x.report('slow code') do |times| | |
i = 0 | |
while i < times | |
slow(100, 200) | |
i += 1 | |
end | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment