Search code examples
pythonperformancecaching

Why do python programs run very slow the first time?


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.

enter image description here

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.


Solution

  • 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.