20 seconds to write how many numbers? If you are doing
create_file_numbers_old(filename, 5)
then 20 seconds is really slow. But if you are doing:
create_file_numbers_old(filename, 50000000000000)
then 20 seconds is amazingly fast.
Try this instead, it may be a little faster:
def create_file_numbers_old(filename, size):
count = value = 0
with open(filename, 'w') as f:
while count < size:
s = '%dn' % value
f.write(s)
count += len(s)
value += 1
If this is still too slow, you can try three other tactics:
1) pre-calculate the largest integer that will fit in size
bytes, then use a for-loop instead of a while loop:
maxn = calculation(...)
with open(filename, 'w') as f:
for i in xrange(maxn):
f.write('%dn' % i)
2) Write an extension module in C that writes to the file.
3) Get a faster hard drive, and avoid writing over a network.