You’re using endl
to print a newline. That is the problem here, as it does more than just printing a newline — endl
also flushes the buffer which is an expensive operation (if you do that in each iteration).
Use \n
if you mean so:
file << i << '\n';
And also, must compile your code in release mode (i.e turn on the optimizations).