Skip to content

Instantly share code, notes, and snippets.

@alebian
Last active February 27, 2019 23:27
Show Gist options
  • Save alebian/724144c98f269d3f0407986f0a8be949 to your computer and use it in GitHub Desktop.
Save alebian/724144c98f269d3f0407986f0a8be949 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
require 'memory_profiler'
N = 50000
PROCS = [
[
"Description 1",
-> { 'Insert code here' }
],
[
"Description 2",
-> { 'Insert code here' }
]
]
puts("Benchmarking #{N} times\n\n")
Benchmark.bm do |x|
PROCS.each do |(description, proc)|
x.report(description) do
N.times { proc.call }
end
end
end
puts("\n")
Benchmark.ips do |x|
PROCS.each do |(description, proc)|
x.report(description) do
N.times { proc.call }
end
end
end
puts("\n")
PROCS.each do |(description, proc)|
puts "Memory usage of #{description} ".ljust(50, '-')
report = MemoryProfiler.report do
proc.call
end
puts "\tTotal allocated: #{report.total_allocated_memsize} bytes (#{report.total_allocated} objects)"
puts "\tTotal retained: #{report.total_retained_memsize} bytes (#{report.total_retained} objects)"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment