Created
November 7, 2021 18:21
-
-
Save bsharpe/0f979efce4e13656f0bd2f04908b40e4 to your computer and use it in GitHub Desktop.
Simple ruby micro benchmark tool with performance and memory reports
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
# 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