Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 as compared to the first time 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.

like image 490
Pratik Singhal Avatar asked Oct 30 '13 14:10

Pratik Singhal


People also ask

Why is my Python program so slow?

In summary: code is slowed down by the compilation and interpretation that occurs during runtime. Compare this to a statically typed, compiled language which runs just the CPU instructions once compilated. It's actually possible to extend Python with compiled modules that are written in C.

Why is Python so popular despite being so slow?

Why Programmers Opt for Python despite Its Slower Speed? Most programmers nowadays focus on the readability and quality of the code to maintain and update the software easily in future. In addition to being simple and easy-to-learn, Python enables programmers to express concepts with concise and readable code.


2 Answers

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.

like image 191
Jon Clements Avatar answered Nov 10 '22 15:11

Jon Clements


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
like image 36
Eser Aygün Avatar answered Nov 10 '22 15:11

Eser Aygün