optimizing for loops