Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created July 5, 2018 16:16
Show Gist options
  • Save katafrakt/cf79b95aaf0b7ba038dda7fdd88cff63 to your computer and use it in GitHub Desktop.
Save katafrakt/cf79b95aaf0b7ba038dda7fdd88cff63 to your computer and use it in GitHub Desktop.
Object creation benchmark
require 'benchmark/ips'
class Service
def call(a, b)
a + b
end
end
Benchmark.ips do |x|
x.report("creation") do
Service.new
end
end
Benchmark.ips do |y|
a = 1
b = 2
y.report("without reuse") do
Service.new.call(a, b)
end
service = Service.new
y.report("with reuse") do
service.call(a, b)
end
end
Warming up --------------------------------------
creation 201.819k i/100ms
Calculating -------------------------------------
creation 5.168M (± 8.9%) i/s - 25.631M in 5.004994s
Warming up --------------------------------------
without reuse 191.692k i/100ms
with reuse 256.987k i/100ms
Calculating -------------------------------------
without reuse 4.352M (± 9.3%) i/s - 21.661M in 5.027314s
with reuse 8.336M (± 6.4%) i/s - 41.632M in 5.014777s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment