Why are python programs so slow for the first time?

I created a simple program that searches for a specific file in a specific directory.
The problem with the program is that it runs very slowly for the first time, but very fast compared to the first when you run it later. I am pasting a screenshot of the same. I would like to know why this is so? I found the same thing on both windows 7, as well as on ubuntu 12.04 LTS, but the difference in speed (or the time difference is large on Windows 7. enter image description here

See the time difference between the second and third searches. First it takes 81.136 seconds, and the second 6.45 seconds, although we are looking for the same directory.

+6
source share
2 answers

It is not related to Python. Scanned files will still be located in the cache of the OS file system, so it does not require as much access to the disk as the first run ...

You can reproduce something like:

with open('a 100mb or so file') as fin: filedata = fin.read() 

In the second run, most likely the file is still in memory, not on the disk, so the second run will be much faster.

+9
source

Modern systems optimize access to recently acquired data using caching mechanisms. This probably happens in your case. So, this is not about Python, but about the operating system and storage.

Here are the results for a basic search operation (which has nothing to do with Python) performed sequentially on my machine.

 time find /usr/ -name java ... real 1m15.946s time find /usr/ -name java ... real 0m24.577s 
+7
source

Source: https://habr.com/ru/post/957040/


All Articles