Switching two performance sensitive foreach loops with faster conventional for loops