我正试图绕过Ruby的慢速File.each_line实现。
def parse
@file = File.open(@file)
@filesize = @file.stat.size
@buffer_size = 10485
@offset = @buffer_size
@counter = 0
@line_counter = 0
@file.seek(0, File::SEEK_END)
while @file.tell > 0
@file.seek(-@offset, File::SEEK_END)
buffer = @file.read(@buffer_size)
buffer_line = buffer.split("\n")[1]
@counter += 1
puts @counter
@offset += @buffer_size
return if @offset > @filesize
end
end
我不确定如何实现缓冲区,我可以存储每行的剩余部分。