Reputation: 6492
I have made a simple program, that searches for a particular file in a particular directory.
The problem with the program is that it runs very slow the first time, but very fast when you run it subsequently. I am pasting a screenshot of the same.I would like to know, why is it so? I have discovered the same thing on both windows 7 as well as ubuntu 12.04 LTS, but the speed difference (or time difference) is great on windows 7.
See the time difference between the second and the third searches. First takes 81.136 seconds and the second one takes 6.45 seconds, although we are searching the same directory.
Upvotes: 12
Views: 5014
Reputation: 8004
Modern systems optimizes access to recently accessed data by using caching mechanisms. This is probably what happens in your case. So, it's not about Python but about the operating system and the storage.
Here are the results for a basic find operation (which has nothing to do with Python) executed consecutively on my machine.
time find /usr/ -name java
...
real 1m15.946s
time find /usr/ -name java
...
real 0m24.577s
Upvotes: 8
Reputation: 142116
It's nothing to do with Python. The files scanned will still be in the OS's file system cache, so don't require as much disk access as the first run...
You could reproduce with something like:
with open('a 100mb or so file') as fin:
filedata = fin.read()
On the second run, it's likely the file is still in memory rather than disk, so the second run will be significantly faster.
Upvotes: 14