On Aug 20, 2007, at 4:33 PM, Marcel Molina Jr. wrote: > streaming 0.270000 0.010000 0.280000 ( 0.293957) > shelling out 0.000000 0.000000 0.020000 ( 0.052078) > > (The file is 234936 lines.) > my attempt: cfp:~ > cat a.rb && ruby a.rb Documents/words.txt && wc -l Documents/ words.txt require 'benchmark' big_file = ARGV.shift || '/usr/share/dict/words' Benchmark.bm do |x| x.report('streaming') do lines = 0 File.open(big_file).each_line do |line| lines += 1 end end x.report('shelling out') do lines = Integer(%x(wc -l '#{big_file}')[/^\d+/]) end x.report('letting ruby do the counting') do lines = open(big_file){|fd| fd.each{} and fd.lineno} end x.report('wow') do lines = open(big_file){|fd| fd.read(fd.stat.size).count "\n"} end x.report('smart') do class File def number_of_lines way_too_big = 2 ** 30 stat.size > way_too_big ? (each{} and lineno) : read(stat.size).count("\n") end end lines = open(big_file){|fd| fd.number_of_lines} end end user system total real streaming 0.420000 0.010000 0.430000 ( 0.436458) shelling out 0.000000 0.000000 0.010000 ( 0.028870) letting ruby do the counting 0.290000 0.010000 0.300000 ( 0.296236) wow 0.010000 0.010000 0.020000 ( 0.025010) smart 0.010000 0.020000 0.030000 ( 0.029373) 483523 Documents/words.txt a @ http://drawohara.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama