loops - Execute statement every N iterations in Python -
i have long loop, , check status every n iterations, in specific case have loop of 10 million elements , want print short report every millionth iteration.
so, doing (n iteration counter):
if (n % 1000000==0): print('progress report...') but worried slowing down process computing modulus @ each iteration, 1 iteration lasts few milliseconds.
is there better way this? or shouldn't worry @ modulus operation?
how keeping counter , resetting 0 when reach wanted number? adding , checking equality faster modulo.
printcounter = 0 # whatever while loop in python while (...): ... if (printcounter == 1000000): print('progress report...') printcounter = 0 ... printcounter += 1 although it's quite possible compiler doing sort of optimization already... may give peace of mind.
Comments
Post a Comment