<div dir="ltr">Hi.<br><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:0 0 0 .8ex;border-left:1px #ccc solid;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>
<br>
I considered, and discredited, the following attempted "explanations":<br>
<br>
1. "The extra 12 milliseconds is the time it takes to read and/or compile<br>
the function's code". This explanation isn't true, because the code is<br>
read and/or compiled already when I "import" the module, before I<br>
measure the first call to the function. Moreover, if I measure parts of<br>
this function's code, I see it becomes uniformly slow - e.g., half of<br>
its code gets done in 6 milliseconds; It's not a 12 millisecond pause<br>
and then the rest of the function finishes in 2ms.<br></blockquote><div><br>Yeah, compiles to bytecode.<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
2. "The code is slower the first time it runs, because only then does it<br>
get compiled and/or optimized". As far as I know, Python doesn't do<br>
JIT so this isn't a correct explanation...<br>
<br></blockquote><div><br>Nitpick: CPython doesn't (the one you're referring to as Python), other implementations may and will (PyPy, Jython, etc)<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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>
<br></blockquote><div><br>A module is loaded only once (see also: sys.modules)<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Any ideas?<br>
<br>
Thanks,<br>
Nadav.<br>
<span class="HOEnZb"></span></blockquote><div><br>Can you post some code ? Without it this may be tough. Some shots in the dark:<br><br>Do you have imports inside the function ?<br>Do you access and affect globals ?<br>Is it a generator ?<br>
Do you have default parameters ? Are they mutable ? Do they require some computation ?<br>Is there a difference in the dataset between the runs (python caches small ints).<br>Does it manipulate files (will be cached by the OS),<br>
<br>Cheers<br></div></div>-- <br><div dir="ltr">Meir Kriheli<br><a href="http://meirkriheli.com" target="_blank">http://meirkriheli.com</a><br></div>
</div>