Operating systems are already optimized for such I / O. Thus, you can directly write numbers to a file and get very good speed:
import tempfile, random with tempfile.NamedTemporaryFile(delete=False) as nf: for _ in xrange(1000000):
In practice, numbers will be written to disk only when the size buffer file is full. The code in question and the above code take the same time on my machine. Therefore, I would advise you to use my simpler code and rely on built-in optimizations of the operating system.
If the result fits into memory (this applies to 1 million numbers), you can really save some I / O by creating the final line and then writing it in one go:
with tempfile.NamedTemporaryFile(delete=False) as nf: nf.write(''.join(str(random.randint(0, 1000)) for _ in xrange(1000000)))
This second approach is 30% faster on my computer (2.6 s instead of 3.8 s), probably due to a single write call (instead of millions of write() calls - and probably a lot less actual disk writes).
The "large number of entries" approach of your question falls in the middle (3.1 s). However, this can be improved: it is clearer and more pythonic to write like this:
import tempfile, random with tempfile.NamedTemporaryFile(delete=False) as nf: for _ in xrange(1000): nf.write(''.join(str(random.randint(0, 1000)) for _ in xrange(1000)))
This solution is equivalent, but faster than the code in the original question (2.6 s on my machine instead of 3.8 s).
So the first, simple approach above may be quick enough for you. If this is not the case, and if the entire file can fit in memory, the second approach will be very fast and simple. Otherwise, your original idea (fewer records, larger blocks) is good, as it is about as fast as a “single record”, and is still pretty simple when written as above.