searching for optimized loop function