Marcel Molina Jr.
8/20/2007 10:34:00 PM
On Tue, Aug 21, 2007 at 07:24:03AM +0900, blufur wrote:
> is there built-in method to determine the number of lines in a file?
>
> i tried file.readlines.length but it is very slow (dealing with files
> > 1 million lines)
Here are a few alternatives that use less memory than File.readlines (which
slurps in the entire file into memory):
require 'benchmark'
big_file = '/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
end
On my machine:
user system total real
streaming 0.270000 0.010000 0.280000 ( 0.293957)
shelling out 0.000000 0.000000 0.020000 ( 0.052078)
(The file is 234936 lines.)
marcel
--
Marcel Molina Jr. <marcel@vernix.org>