Hi, I have run across a puzzling issue in Python, and I wonder if anyone
on the list can explain it.

I have a python function which takes some input and produces some
output - it doesn't keep permanent state, and presumably running it
twice would do exactly the same thing twice, and take exactly the same
time.

But strangely, it doesn't - the first call takes 14 milliseconds, while
the second and all subsequent calls take only 2 milliseconds each.
Does anybody have any idea why this can happen?

I considered, and discredited, the following attempted "explanations":

1. "The extra 12 milliseconds is the time it takes to read and/or compile
   the function's code". This explanation isn't true, because the code is
   read and/or compiled already when I "import" the module, before I
   measure the first call to the function. Moreover, if I measure parts of
   this function's code, I see it becomes uniformly slow - e.g., half of
   its code gets done in 6 milliseconds; It's not a 12 millisecond pause
   and then the rest of the function finishes in 2ms.

2. "The code is slower the first time it runs, because only then does it
   get compiled and/or optimized". As far as I know, Python doesn't do
   JIT so this isn't a correct explanation...

3. "If class A imports B which imports C which imports D, some of these
   classes are only read when the code is actually used for the first
   time". Again, I couldn't find any evidence that this is true in Python
   (unlike, e.g., Java). An "import" would read the whole class hierarchy into
   memory. Right?

Any ideas?

Thanks,
Nadav.

-- 
Nadav Har'El                        |                 Wednesday, Jun 20 2012, 
n...@math.technion.ac.il             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |I am thinking about a new signature. Stay
http://nadav.harel.org.il           |tuned.

_______________________________________________
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il

Reply via email to