kissgyorgy
kissgyorgy

Reputation: 3010

Why is Python script reading files much faster the second run?

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

Answers (3)

Jon-Eric
Jon-Eric

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

Martijn Pieters
Martijn Pieters

Reputation: 1122562

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

Upvotes: 5

phihag
phihag

Reputation: 287915

That's the disk cache of the operating system.

Upvotes: 6

Related Questions