Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created August 1, 2013 01:10
Show Gist options
  • Select an option

  • Save invisiblefunnel/6127679 to your computer and use it in GitHub Desktop.

Select an option

Save invisiblefunnel/6127679 to your computer and use it in GitHub Desktop.
Relative require times of equivalent syntax
require 'benchmark'
require 'fileutils'
require 'debugger'
def N(&block)
100_000.times(&block)
end
CASES = {
'class-definition' => [
proc{|n| "class A#{n}; end\n"},
proc{|n| "A#{n} = Class.new{}\n"}
],
'hash-initialization' => [
proc{"Hash.new\n"},
proc{"{}\n"}
],
}
CASES.each do |name, (case_a, case_b)|
case_a_file, case_b_file = "#{name}-a.rb", "#{name}-b.rb"
case_a_file_path, case_b_file_path = "./#{case_a_file}", "./#{case_b_file}"
File.open(case_a_file, 'w') { |f| N{ |n| f.write(case_a.call(n)) } }
File.open(case_b_file, 'w') { |f| N{ |n| f.write(case_b.call(n)) } }
Benchmark.bmbm do |x|
x.report("Case A: #{name}") do
fork { require case_a_file_path }
Process.waitall
end
x.report("Case B: #{name}") do
fork { require case_b_file_path }
Process.waitall
end
end
FileUtils.rm case_a_file_path
FileUtils.rm case_b_file_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment