Search code examples
pythoniopython-internals

How did Python read this binary faster the second time?


I'm exploring methods for reading a formatted binary file and was starting with the basics:

>>> with open(fp, 'rb') as f:
        buffer = f.read()

My file is 1.02GB and it took ~90 seconds to read it and store in memory the first time. By chance I accidentally told the interpreter to read the file again (hit Enter too many times) and it read it in 0.5 seconds. The file was closed prior to re-reading unintentionally.

Whats going on here?


Solution

  • Most likely the file data was cached and still in memory. Most operating systems will leave data in memory for a while, in case it is asked for again.