From: Daniel Berger Date: 2003-04-11T02:37:05+09:00 Subject: Re: Need IO Optimization help Jim Freeze wrote: > > On Friday, 11 April 2003 at 2:16:50 +0900, Yukihiro Matsumoto wrote: > > Hi, > > > > In message "Need IO Optimization help" > > on 03/04/11, Jim Freeze writes: > > > > |We're having a little shoot out here at work with Ruby, Perl and Tcl. > > |So far, Ruby kicked on a recursive Fibonacci(sp?) sequence with > > |Perl about 50% slower and Tcl 10x slower. FWIW, here is a benchmark suite you can use: ############################################################ # iobenchmark.rb - benchmark to test IO read/write methods ############################################################ require "benchmark" include Benchmark outfile1 = "test1.out" outfile2 = "test2.out" outfile3 = "test3.out" outfile4 = "test4.out" outfile5 = "test5.out" outfile6 = "test6.out" outfile7 = "test7.out" outfile8 = "test8.out" outfile9 = "test9.out" outfiles = Dir["test*.out"] outfiles.each{ |f| File.delete(f) } string = "The quick brown fox jumped over the lazy dog's back\n" string_len = string.length # 52 iterations = 100000 # About 5mb per 100,000 iterations bm do |x| x.report("print:"){ iterations.times{ File.open(outfile1,"a+"){ |f| f.print(string) } } } x.report("write:"){ iterations.times{ File.open(outfile2,"a+"){ |f| f.write(string) } } } x.report("syswrite:"){ iterations.times{ File.open(outfile3,"a+"){ |f| f.syswrite(string) } } } x.report("IO.foreach:"){ outfile = File.open(outfile4,"a+") IO.foreach(outfile1){ |line| outfile.syswrite(line) } outfile.close } x.report("File::gets:"){ infile = File.open(outfile1) outfile = File.open(outfile5,"a+") while line = infile.gets outfile.syswrite(line) end infile.close outfile.close } x.report("File::readline:"){ infile = File.open(outfile1) outfile = File.open(outfile6,"a+") begin while line = infile.readline outfile.syswrite(line) end rescue EOFError # do nothing end infile.close outfile.close } x.report("File::read:"){ infile = File.open(outfile1) outfile = File.open(outfile7,"a+") begin while line = infile.read(string_len) outfile.syswrite(line) end rescue EOFError # do nothing end infile.close outfile.close } x.report("File::sysread:"){ infile = File.open(outfile1) outfile = File.open(outfile8,"a+") begin while line = infile.sysread(string_len) outfile.syswrite(line) end rescue EOFError # do nothing end infile.close outfile.close } x.report("File::each:"){ outfile = File.open(outfile9,"a+") File.open(outfile1).each{|line| outfile.syswrite(line) } outfile.close } end