time-consuming loop optimized