# of lines in a file

B

blufur

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)

thanks,
DAN
 
M

Marcel Molina Jr.

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

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
 
J

Jano Svitok

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

if on unix:
`wc -l #{filename}` or similar (I don't remember the exact syntax for wc)

otherwise:

try counting \r\n or \n. Read file in a loop, counting the occurences.
There was a thread recently how to process a file as fast as possible
-- search the archives.
 
C

Chad Perrin

if on unix:
`wc -l #{filename}` or similar (I don't remember the exact syntax for wc)

Your use of syntax is correct, there. The -l option tells wc to only
report the number of lines.
 
A

ara.t.howard

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/
 
R

Ronald Fischer

if on unix:
syntax for wc)
=20
Your use of syntax is correct, there. The -l option tells wc to only
report the number of lines.

Nearly correct. It also prints out the filename. A better approach
when calling from Ruby would be

linecount=3D`wc -l <#{filename}`.chomp.to_i

--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top