Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
## benchmark 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 <stdio.h> #include <stddef.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> 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) ## rbx with new stat_file primitive euler:rubinius brian$ rm stat.rbc; TOTAL=1_000 shotgun/rubinius stat.rb Rehearsal ------------------------------------------------------ loop 0.536307 0.000000 0.536307 ( 0.536293) Stat.stat 12.438195 0.000000 12.438195 ( 12.438186) File.stat 19.902529 0.000000 19.902529 ( 19.902510) File.exist? 20.560152 0.000000 20.560152 ( 20.560134) File.file? 38.015128 0.000000 38.015128 ( 38.015113) C stat (1_645_000) 6.382751 0.000000 6.382751 ( 6.382689) -------------------------------------------- total: 97.835062sec user system total real loop 0.541292 0.000000 0.541292 ( 0.541290) Stat.stat 12.733344 0.000000 12.733344 ( 12.733348) File.stat 19.946062 0.000000 19.946062 ( 19.946066) File.exist? 20.518921 0.000000 20.518921 ( 20.518929) File.file? 38.138579 0.000000 38.138579 ( 38.138587) C stat (1_645_000) 6.362458 0.000000 6.362458 ( 6.362386) ## rbx with old file_stat primitive euler:rubinius brian$ rm stat.rbc; TOTAL=1_000 shotgun/rubinius stat.rb Rehearsal -------------------------------------------------- loop 0.528784 0.000000 0.528784 ( 0.528767) File.stat 30.653916 0.000000 30.653916 ( 30.653916) File.file? 31.231470 0.000000 31.231470 ( 31.231460) FileTest.file? 31.790378 0.000000 31.790378 ( 31.790374) ---------------------------------------- total: 94.204548sec user system total real loop 0.521969 0.000000 0.521969 ( 0.521974) File.stat 30.686709 0.000000 30.686709 ( 30.686724) File.file? 31.285714 0.000000 31.285714 ( 31.285719) FileTest.file? 31.806415 0.000000 31.806415 ( 31.806424) ## memory with FFI 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 ## results euler:rubinius brian$ rm memory.rbc; TOTAL=100_000 shotgun/rubinius memory.rb Rehearsal -------------------------------------------------- loop 0.006613 0.000000 0.006613 ( 0.006615) MemPtr(num) 1.170544 0.000000 1.170544 ( 1.170535) MemPtr(struct) 1.246660 0.000000 1.246660 ( 1.246643) Struct 0.197862 0.000000 0.197862 ( 0.197812) ----------------------------------------- total: 2.621679sec user system total real loop 0.006349 0.000000 0.006349 ( 0.006370) MemPtr(num) 1.107966 0.000000 1.107966 ( 1.107970) MemPtr(struct) 1.222053 0.000000 1.222053 ( 1.222051) Struct 0.183214 0.000000 0.183214 ( 0.183211)
From the Design Piracy series on my blog: