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.
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.
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 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With