1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#######################################################################
# bench_dir.rb
#
# A series of benchmarks for the Dir class.
#######################################################################
require "benchmark"
MAX = 40000
PWD = Dir.pwd
Dir.rmdir('test_dir') if File.exists?('test_dir')
Benchmark.bm(30) do |x|
x.report("Dir.chdir"){
MAX.times{ Dir.chdir(PWD) }
}
x.report("Dir.chdir{}"){
MAX.times{ Dir.chdir(PWD){} }
}
x.report("Dir.mkdir & Dir.delete"){
MAX.times{ |n|
Dir.mkdir('test_dir')
Dir.delete('test_dir')
}
}
x.report("Dir.entries"){
MAX.times{ Dir.entries(PWD) }
}
x.report("Dir.foreach"){
MAX.times{ Dir.foreach(PWD){} }
}
x.report("Dir.getwd"){
MAX.times{ Dir.getwd }
}
x.report("Dir.glob('*')"){
MAX.times{ Dir["*"] }
}
x.report("Dir.glob('**/*')"){
MAX.times{ Dir["**/*"] }
}
x.report("Dir.new + Dir#close"){
MAX.times{
Dir.new(PWD).close
}
}
x.report("Dir.open"){
MAX.times{ Dir.open(PWD){} }
}
x.report("Dir#each"){
dir = Dir.new(PWD)
MAX.times{ dir.each{} }
dir.close
}
x.report("Dir#path"){
dir = Dir.new(PWD)
MAX.times{ dir.path }
dir.close
}
x.report("Dir#read + Dir#pos"){
dir = Dir.new(PWD)
MAX.times{
dir.read
dir.pos
}
dir.close
}
x.report("Dir#read + Dir#pos="){
dir = Dir.new(PWD)
MAX.times{
5.times{ dir.read }
dir.pos = 2
}
dir.close
}
x.report("Dir#read + Dir#rewind"){
dir = Dir.new(PWD)
MAX.times{
5.times{ dir.read }
dir.rewind
}
dir.close
}
end
|