Skip to content

Instantly share code, notes, and snippets.

@jbodah
Created April 9, 2015 18:31
Show Gist options
  • Select an option

  • Save jbodah/2d066cd6801ebb742773 to your computer and use it in GitHub Desktop.

Select an option

Save jbodah/2d066cd6801ebb742773 to your computer and use it in GitHub Desktop.
performance benchmark
require 'benchmark'
def performance_benchmark(name, &block)
# 31 runs, we'll discard the first result
(0..30).each do |i|
# force GC in parent process to make sure we reclaim
# any memory taken by forking in previous run
GC.start
# fork to isolate our run
pid = fork do
# again run GC to reduce effects of forking
GC.start
# disable GC if you want to see the raw performance of your code
GC.disable if ENV["RUBY_DISABLE_GC"]
# because we are in a forked process, we need to store
# results in some shared space. local file is the simplest way to do that
benchmark_results = File.open("benchmark_results_#{name}", "a")
elapsed_time = Benchmark::realtime do
yield
end
# do not count the first run
if i > 0
# we use system clock for measurements,
# so microsecond is the last significant figure
benchmark_results.puts elapsed_time.round(6)
end
benchmark_results.close
GC.enable if ENV["RUBY_DISABLE_GC"]
end
Process::waitpid pid
end
measurements = File.readlines("benchmark_results_#{name}").map do |value|
value.to_f
end
File.delete("benchmark_results_#{name}")
average = measurements.inject(0) { |sum, x| sum + x }.to_f / measurements.size
stddev = Math.sqrt(
measurements.inject(0){ |sum, x| sum + (x - average)**2 }.to_f /
(measurements.size - 1)
)
# return both average and standard deviation, this time in millisecond precision
# for all practical purposes that should be enough
[name, average.round(3), stddev.round(3)]
end
@jbodah

jbodah commented Apr 9, 2015

Copy link
Copy Markdown
Author

@jbodah

jbodah commented Apr 9, 2015

Copy link
Copy Markdown
Author

Usage:

require 'performance_benchmark'

result = performance_benchmark("sleep 1 sec") do
  sleep 1
end

puts "%-28s %0.3f ± %0.3f" % result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment