Reputation: 10033
I have this simple python
code:
import numpy as np
a = np.array([1,2,3,4,5,6])
print a
If I run it over and over on sublime text
, with (cmd+B)
it prints the result [1 2 3 4 5 6]
faster each time, like so (in sec):
1st run: [Finished in 7.7s]
2nd run: [Finished in 1.6s]
3rd run: [Finished in 0.5s]
then it stabilizes at 0.5s
, which is pretty slow, for such a simple program.
so I tried to time it, and got the following results:
[1 2 3 4 5 6]
0.00280284881592
[Finished in 7.4s]
[1 2 3 4 5 6]
0.00325489044189
[Finished in 2.0s]
[1 2 3 4 5 6]
0.000813961029053
[Finished in 0.1s]
testing further, I timed the code bypassing package import:
a = [1,2,3,4,5,6];
print(a)
and got:
[1, 2, 3, 4, 5, 6]
0.000219106674194
[Finished in 1.4s]
and finally, with numpy
:
import numpy as np
a = np.array([1,2,3,4,5,6])
a=1
I got:
1.2614569664
[Finished in 2.5s]
what is happening with computer memory so the program behaves like this?
Upvotes: 3
Views: 300
Reputation: 8629
I think there are 3 main questions in your post:
The reason is that OS will cache frequently used files from disk to RAM. And RAM access is much more fast than disk access. This is called disk cache or page cache. After run python multiple times, the memory is warm, OS can load related files from fast RAM. To be more precise, it won't be faster each time continously. Instead, after two or three times, it reach the fastest.
The time after 'Finished in' given by sublime text includes 2 parts: one is the time used to start python
itself, the other is time running script.
In your case, most time consists of time starting python
and time importing numpy package. There are some cases in my machine. All the time is "stablized" time.
case 1:
import numpy as np
print(0)
[Finished in 0.7s]
case 2:
print(0)
[Finished in 0.2s]
case 3:
import datetime
print(0)
[Finished in 0.2s]
As shown above, starting python cost 0.2s, importing numpy cost 0.5s. I think numpy is so huge that importing is slow.
Because disk cache will be adjusted dynamically according to latest access. After 10 minutes, the memory is taken by other programs in your computer. Memory cools down.
Upvotes: 2