3

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 

3 Answers 3

6

That's the disk cache of the operating system.

Sign up to request clarification or add additional context in comments.

Comments

5

It's the operating system. Modern OSes use disk caches to speed up repeated reads.

Comments

4

Your operating system probably cached the data from disk after the first run. Restart your computer and see how the run time compares.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.