reading from file only once, instead of for every iteration