require 'benchmark'
total = (ENV['TOTAL'] || 1_000).to_i
strings = Dir["spec/**/*_spec.rb"]
Benchmark.bmbm do |x|
x.report("loop") do
times = 0
while times < total
strings.each { |s| s }
times += 1
end
end
x.report("Stat.stat") do
Stat = File::Stat
times = 0
while times < total
strings.each { |s| Stat.stat s, true }
times += 1
end
end
x.report("File.stat") do
times = 0
while times < total
strings.each { |s| File.stat s }
times += 1
end
end
x.report("File.exist?") do
times = 0
while times < total
strings.each { |s| File.exist? s }
times += 1
end
end
x.report("File.file?") do
times = 0
while times < total
strings.each { |s| File.file? s }
times += 1
end
end
x.report("C stat (1_645_000)") do
`./stat`
end
end
stat.c
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
struct stat *st, sb;
int i, total = 1000*1645;
if(argc > 1) {
st = malloc(sizeof(struct stat));
} else {
st = &sb;
}
for(i = 0; i < total; i++) {
stat("./spec/ruby/1.8/core/true/and_spec.rb", st);
}
return 0;
}
gcc -o stat stat.c
ruby
euler:rubinius brian$ TOTAL=1_000 ruby stat.rb
Rehearsal ------------------------------------------------------
loop 0.340000 0.000000 0.340000 ( 0.379366)
File.stat 5.350000 7.160000 12.510000 ( 12.629583)
File.exist? 4.280000 6.770000 11.050000 ( 11.173089)
File.file? 4.260000 6.650000 10.910000 ( 10.946640)
C stat (1_645_000) 0.000000 0.000000 6.360000 ( 6.378908)
-------------------------------------------- total: 41.170000sec
user system total real
loop 0.350000 0.010000 0.360000 ( 0.351319)
File.stat 5.360000 7.120000 12.480000 ( 12.610997)
File.exist? 4.320000 6.590000 10.910000 ( 10.950847)
File.file? 4.280000 6.610000 10.890000 ( 11.923850)
C stat (1_645_000) 0.000000 0.000000 6.380000 ( 6.659911)
require 'benchmark'
total = (ENV['TOTAL'] || 1_000).to_i
Benchmark.bmbm do |x|
x.report("loop") do
times = 0
while times < total
times += 1
end
end
x.report("MemPtr(num)") do
S = File::Stat::StructStat
times = 0
while times < total
MemoryPointer.new(96) do |m|
m
end
times += 1
end
end
x.report("MemPtr(struct)") do
S = File::Stat::StructStat
times = 0
while times < total
MemoryPointer.new(S) do |m|
m
end
times += 1
end
end
x.report("Struct") do
S = File::Stat::StructStat
m = MemoryPointer.new S
times = 0
while times < total
S.new m
times += 1
end
end
end