Hi. On Wed, Jun 20, 2012 at 12:27 AM, Nadav Har'El <n...@math.technion.ac.il>wrote:
> 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. > Yeah, compiles to bytecode. > > 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... > > Nitpick: CPython doesn't (the one you're referring to as Python), other implementations may and will (PyPy, Jython, etc) > 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? > > A module is loaded only once (see also: sys.modules) > Any ideas? > > Thanks, > Nadav. > Can you post some code ? Without it this may be tough. Some shots in the dark: Do you have imports inside the function ? Do you access and affect globals ? Is it a generator ? Do you have default parameters ? Are they mutable ? Do they require some computation ? Is there a difference in the dataset between the runs (python caches small ints). Does it manipulate files (will be cached by the OS), Cheers -- Meir Kriheli http://meirkriheli.com
_______________________________________________ Linux-il mailing list Linux-il@cs.huji.ac.il http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il