I have a loop that looks like this
def slow_loop(array)
array.each_with_index do |item, i|
next_item = array[i+1]
if next_item && item.attribute == next_item.attribute
do_something_with(next_item)
end
end
end
aside from changing the way do_something_with is called, how can i make this perform better?
thx,
-C
p.s.
Since it appears that this is an ‘O(n)’ operation, there is apparently no performance to be gained here, so the answer i chose is one that uses a ruby method that already encapsulates this operation. thanks for your help everybody
As others have mentioned you’re not going to improve the performance much, but you could do this more cleanly like so: