I don’t think it will be much faster.
However, some tests show that a significant part of the time is spent on transcoding (about 15% for my test). Therefore, if you can skip this (for example, by creating CSV in UTF-8 already), you will see some improvements.
, ruby-doc.org, "" CSV
foreach, :
def csv_import
import 'csv'
CSV.foreach("/#{Rails.public_path}/uploads/shate.csv", {:encoding => 'ISO-8859-15:UTF-8', :col_sep => ';', :row_sep => :auto, :headers => :first_row}) do | row |
end
end
. , ( ):
N = 10000
def csv_import
all_lines = File.read("/#{Rails.public_path}/uploads/shate.csv").lines
parts, threads = [], []
all_lines.each_slice(N) do | plines |
parts << result = []
threads << Thread.new(plines.join, result) do | tsrc, tresult |
parsed = CSV.parse(tsrc, {:encoding => 'ISO-8859-15:UTF-8', :col_sep => ";", :row_sep => :auto})
tresult.replace(parsed.to_a)
end
end
threads.each(&:join)
parts.flatten(1).each do | row |
end
end
10000 . parts . ( threads.each(&:join)), parts .