I am trying to find the most efficient way to process lines in a Ruby string in reverse order. These are the two approaches I have:
def double_reverse(lines)
lines.reverse!
lines.each_line do |line|
line.chomp!
line.reverse!
puts line
end
end
def split_and_reverse(lines)
lines.split("\n").reverse.each do |line|
puts line
end
end
if __FILE__ == $0
lines = "This is the first line.\nThis is the second line"
double_reverse(lines)
lines = "This is the first line.\nThis is the second line"
split_and_reverse(lines)
end
I am wondering which one will use less memory. Is there any other approach which will use even less resource? I am primarily concerned about memory usage but if I can reduce the CPU usage too that would be nice.
EDIT 1:
In my use case lines
can have more than a million lines. If split
is going to increase the memory usage by 2x then it is definitely a problem for me. But it may not be a problem if the Ruby VM is smart enough to determine that lines
won't be used after the call to split
and releases it's memory. On the other hand the in-place reverse!
approach theoretically seems to be more efficient since it can be done without making any copy of lines
.