Last active
February 27, 2019 23:27
-
-
Save alebian/724144c98f269d3f0407986f0a8be949 to your computer and use it in GitHub Desktop.
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
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