Does calling `f.write` so many times cause a lot of I/O? I wonder if it would be safer to just generate 1 MB in memory and write it once.