Import Question

E

eli m

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.
 
M

Matteo Boscolo

Il 20/02/2013 21:53, eli m ha scritto:
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.
It depend of your code module code..

if inside your module there is some code (no def or class) this code
will be executed, and if for example you have some loop or some db query
this will be executed too.


regards,
Matteo
 
M

Michael Herman

you can check each import as it varies in loading time: time python -c
"import [name of module]"

example: time python -c "import flask"
 
D

Dave Angel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top