Put some function calls outside of foreach iteration