-1

I have tried length method to get the row count and it took at least 2 mins to return the row count. Length method is fast up to 10 000 and it's too slow when it exceeds 100 000 rows. Is there a way to get the row count with in 2 sec even file contains over 100k rows?

I have used the following code.

file = params[:file]
f = File.open(file.path, 'r:iso-8859-1:utf-8')
csv_file = CSV.parse(f.read, :headers => true, :converters => :all, :header_converters => lambda {|h| h.to_s.include?(' ') ? h.downcase.parameterize.underscore : h})
csv_file.length
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

Eventually I found solution, CSV.read(file.path).length is faster than CSV.parse(f.read).length

  • Your question is about speed, but there can be memory problem if you have over 100k rows and use `CSV.read` so I'm still looking for better solution – kangkyu Nov 27 '18 at 01:36