Reputation: 3010
I read the first line of a couple hundred text files with Python.
This runs under a couple seconds on the first time, but it takes only milliseconds to run the second time!!
Why is that ? Is it python ? Is it the operating system (Windows 7 in my case) ?
Here is the script:
import glob
from datetime import datetime
start = datetime.now()
for summary in glob.glob(r"C:\folder\*.txt"):
with open(summary) as f:
line = f.readline()
print line.rstrip().decode('utf-16')
print 'Time: ', datetime.now()-start
Upvotes: 3
Views: 1857
Reputation: 17275
Your operating system probably cached the data from disk after the first run. Restart your computer and see how the run time compares.
Upvotes: 4
Reputation: 1122562
It's the operating system. Modern OSes use disk caches to speed up repeated reads.
Upvotes: 5