Proposal: Inline Import

Shane Hathaway shane at
Sat Dec 10 09:16:00 CET 2005

Kent Johnson wrote:
> Shane Hathaway wrote:
>>I'm talking about using imports *everywhere*.  The penalty would be 
> Have you tried it?
> D:\Projects\CB>python -m timeit -s "import re" "import re"
> 1000000 loops, best of 3: 1.36 usec per loop
> You need a lot of imports before 1 usec becomes "appreciable".

Let me fully elaborate the heresy I'm suggesting: I am talking about 
inline imports on every other line of code.  The obvious implementation 
would drop performance by a double digit percentage.

> And your 
> proposal is doing the import anyway, just under the hood. How will you 
> avoid the same penalty?

The more complex implementation, which I suggested in the first message, 
is to maintain a per-module dictionary of imported objects (distinct 
from the global namespace.)  This would make inline imports have almost 
exactly the same runtime cost as a global namespace lookup.

But never mind, this proposal is a distraction from the real issue.  See 
the next thread I'm starting.


More information about the Python-list mailing list