Skip to content

Instantly share code, notes, and snippets.

@gromnitsky
Created May 13, 2011
Embed
What would you like to do?
Ruby 'require' time of blank .rb files
# -*-ruby-*-
require 'benchmark'
STEP = 500
ROUNDS = 8
FILES = 'rec-files'
FILES_N = (1..ROUNDS).inject(0) {|i,j| i + j*STEP }
#FILES_LEN = Math.log(FILES_N, 10).floor + 1 # works only in 1.9
FILES_LEN = FILES_N.to_s.size
def rec_file_name(where, n)
"%s/bogus-%0#{FILES_LEN}d.rb" % [where, n]
end
directory FILES
task :depend => FILES do |t|
FILES_N.times { |i| File.new rec_file_name(t.prerequisites[0], i), 'w+' }
end
task :benchmark => [:depend] do
$LOAD_PATH.unshift('.')
puts RUBY_DESCRIPTION
Benchmark.bm do |b|
(1..ROUNDS).inject(0) do |file, n|
b.report("#{n * STEP} requires") {
(n*STEP).times {
require rec_file_name(FILES, file)
file += 1
}
}
file
end
end
end
task :clean do
rm_rf FILES
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment