From: Yohanes Santoso Date: 2002-12-06T06:18:46+09:00 Subject: Re: speeding up ... --=-=-= Mark Probert writes: > f = File.readlines(ARGV[0]) I think if you use #sysread, it will improve the time too. jenny:~> ruby tmp.rb rfc2571.txt user system total real using readlines 5.460000 0.140000 5.600000 ( 5.720842) using sysread 4.720000 0.180000 4.900000 ( 5.006272) The time difference is small since most of the time is spent in manipulating Strings and Array. However, with larger files, the difference will become obvious, especially in simple operations like file copying, etc. YS. --=-=-= Content-Disposition: attachment; filename=tmp.rb Content-Description: test code #! /usr/bin/ruby # def use_readlines(fname) f = File.readlines(fname) nl = f.length nw = nc = 0 f.each { |line| nw += line.split.size nc += line.length } #puts " #{nl} #{nw} #{nc} #{ARGV[0]}" end def use_sysread(fname) lines = "" begin File.open(fname) {|f| while buf = f.sysread(4096) lines << buf end } rescue ensure lines = lines.split("\n") end nl = lines.length nw = nc = 0 lines.each{|line| nw += line.split.size nc += line.length+1 # for the stripped \n } #puts " #{nl} #{nw} #{nc} #{ARGV[0]}" end require 'benchmark' include Benchmark if ARGV.size != 1 puts "usage: #{$0} " exit(1) end n=100 bm{|x| x.report("using readlines") {n.times{use_readlines(ARGV[0])}} x.report("using sysread") {n.times{use_sysread(ARGV[0])}} } --=-=-=--