Skip to content

Instantly share code, notes, and snippets.

@bsharpe
Created November 7, 2021 18:21
Show Gist options
  • Save bsharpe/0f979efce4e13656f0bd2f04908b40e4 to your computer and use it in GitHub Desktop.
Save bsharpe/0f979efce4e13656f0bd2f04908b40e4 to your computer and use it in GitHub Desktop.
Simple ruby micro benchmark tool with performance and memory reports
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "benchmark-ips"
gem "benchmark-memory"
end
require "benchmark/ips"
NAME_A = "inline array"
def method_one
array = %w[one two three four five six seven eight nine ten]
array.include?("four")
end
NAME_B = "constant array"
ARRAY = %w[one two three four five six seven eight nine ten]
def method_two
ARRAY.include?("four")
end
Benchmark.ips do |x|
x.report(NAME_A) { method_one }
x.report(NAME_B) { method_two }
x.compare!
end
Benchmark.memory do |x|
x.report(NAME_A) { method_one }
x.report(NAME_B) { method_two }
x.compare!
end
:done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment