mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Calling #call in a custom class instead of a lambda performs much
better:
require 'benchmark/ips'
class RawFormatter
def call(_severity, _timestamp, _progname, msg) msg end
end
Benchmark.ips do |x|
message = 'my message'
proc = ->(_severity, _timestamp, _progname, msg) { msg }
instance = RawFormatter.new
x.report('proc') do
proc.call(nil, nil, nil, message)
end
x.report('instance') do
instance.call(nil, nil, nil, message)
end
x.compare!
end
# Warming up --------------------------------------
# proc 159.882k i/100ms
# instance 182.648k i/100ms
# Calculating -------------------------------------
# proc 4.612M (± 5.2%) i/s - 23.023M in 5.005950s
# instance 7.716M (± 6.3%) i/s - 38.539M in 5.015306s
#
# Comparison:
# instance: 7716097.7 i/s
# proc: 4611920.5 i/s - 1.67x slower