I have a code, containing many loop iterations, with
As usual, you should profile your specific code to see what are the bottlenecks. However, in general, opening and closing files is very expensive.
Consider the following:
def foo(): f = open('bar.txt', 'w') for i in range(1000): f.write('a') f.close() def bar(): for i in range(1000): f = open('bar.txt', 'w') f.write('a') f.close()
Let's time it:
>>> %timeit foo() 10000 loops, best of 3: 190 µs per loop >>> %timeit bar() 10 loops, best of 3: 47.8 ms per loop
So, opening and closing is extremely expensive.
What are the advantages (or at least mitigating factors) for the constant opens and closes?
Less open file descriptors.
When you close a file, the data is flushed to it. Of course you could just call
flush, but that is an expensive operation in itself, and the time differences would become narrower.
If you don't have critical data (i.e., you can just rerun the program if it crashes), and don't have too many open file descriptor problems - few opens and closes will probably be faster.