Import Question
Dave Angel
davea at davea.name
Wed Feb 20 16:23:44 EST 2013
On 02/20/2013 03:53 PM, eli m wrote:
> How long does it take for the program to import something? I am asking this because i have like 7 imports at the beginning of my program and i am thinking thats the reason why it is slow to start up. Thanks in advance.
>
That would be easy to measure. If you just want a visible indication,
put a print before and after those imports, and see how long between.
Or use the time module to measure it.
Notice that it's possible to write a module that consumes hours during
import. All top level code will be executed. So if there's any
substantial code there that's not conditional, it'll cost you.
For the rest of the message, I'll assume you don't have any
time-consuming initialization code.
Since there are dozens of imports that happen before your code even
begins, I doubt if your own imports make that much difference. Further,
some of them are probably already imported, and it's very fast to
"import" something already in the cache. About as long as a dict lookup
followed by an assignment.
When I look at
len(list(sys.modules.iterkeys()))
in Python 2.7.3, I get the value 243 Some of them are binary modules,
which load pretty much instantaneously, and the others are probably
precompiled, but I still expect them to swamp the 7 you're doing. BTW,
if you measure it, be aware that the first time you import a module, it
must be compiled, but then it saves as a .pyc file, and the next time
it'll be much quicker.
--
DaveA
More information about the Python-list
mailing list