Move the variables startTime and endTime out of for loop.