<div dir="ltr"><font face="georgia,serif"><br></font><br><div class="gmail_quote">On Wed, Jun 20, 2012 at 12:27 AM, Nadav Har'El <span dir="ltr"><<a href="mailto:nyh@math.technion.ac.il" target="_blank">nyh@math.technion.ac.il</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi, I have run across a puzzling issue in Python, and I wonder if anyone<br>
on the list can explain it.<br>
<br>
I have a python function which takes some input and produces some<br>
output - it doesn't keep permanent state, and presumably running it<br>
twice would do exactly the same thing twice, and take exactly the same<br>
time.<br>
<br>
But strangely, it doesn't - the first call takes 14 milliseconds, while<br>
the second and all subsequent calls take only 2 milliseconds each.<br>
Does anybody have any idea why this can happen?<br></blockquote><div><br>Guessing wildly (I don't know much about Python's inner workings, thinking system here): maybe some libraries get dynamically loaded into memory the first time and reused later? <br>
</div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
I considered, and discredited, the following attempted "explanations":<br>
<br></blockquote><div><...> <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
3. "If class A imports B which imports C which imports D, some of these<br>
classes are only read when the code is actually used for the first<br>
time". Again, I couldn't find any evidence that this is true in Python<br>
(unlike, e.g., Java). An "import" would read the whole class hierarchy into<br>
memory. Right?<br></blockquote><div><br>I would - naively! - think that "stuff (not only, or necessarily, the python code stuff, but also what it needs from the system) would indeed be loaded dynamically when it is needed. I don't base this on anything I know, but on the intuitive notion that whatever Python does works as "normal C libraries" - it's implemented in C, right?<br>
<br>Another (kinda related) possibility - some cacheing going on? <br><br>What happens if you run the program twice - is the first call slow every time or just the first time?<br><br>In general, t does not surprise me that something - whatever - runs slower the first time than subsequent times - it happens often.<br>
<br></div></div>-- <br>Oleg Goldshmidt | <a href="mailto:oleg@goldshmidt.org" target="_blank">pub@goldshmidt.org</a><br>
</div>