Created
May 12, 2025 18:47
-
-
Save bensheldon/d33e424d01c31d041653daddd0c9e989 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require "bundler/inline" | |
# Inline Gemfile for dependencies | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "benchmark-ips" | |
end | |
class MyClass | |
NUMBERS = [1, 2, 3, 4, 5].freeze | |
def as_block | |
NUMBERS.map do |number| | |
some_method(number) | |
end | |
end | |
def as_bound_method | |
NUMBERS.map(&method(:some_method)) | |
end | |
def some_method(number) | |
"Hello, World! #{number}" | |
end | |
end | |
require "benchmark/ips" | |
instance = MyClass.new | |
Benchmark.ips do |x| | |
x.report("as_block") { instance.as_block } | |
x.report("as_bound_method") { instance.as_bound_method } | |
x.compare! | |
end |
Author
bensheldon
commented
May 12, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment