Skip to content

Instantly share code, notes, and snippets.

@marocchino
Created February 1, 2019 01:26
Show Gist options
  • Save marocchino/9eeb5d6950cce2a7df0245507170fe29 to your computer and use it in GitHub Desktop.
Save marocchino/9eeb5d6950cce2a7df0245507170fe29 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'benchmark/ips'
def fast
[1, 2].min.then { |m| [m, 3].min }
end
def slow
[1, 2, 3].min
end
Benchmark.ips do |x|
x.report('min with optimize') { fast }
x.report('just min') { slow }
x.compare!
end
# Warming up --------------------------------------
# min with optimize
# 260.309k i/100ms
# just min
# 315.843k i/100ms
# Calculating -------------------------------------
# min with optimize
# 6.130M (± 3.4%) i/s - 30.716M in 5.016724s
# just min
# 9.670M (± 3.0%) i/s - 48.324M in 5.002336s
#
# Comparison:
# min with optimize: 9669767.0 i/s
# just min: 6130468.3 i/s - 1.58x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment