Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39380

Re: Import Question

Date 2013-02-20 16:23 -0500
From Dave Angel <davea@davea.name>
Subject Re: Import Question
References <c5a35a50-320e-4c1f-a825-c80becd9d20f@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2129.1361395441.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Import Question eli m <techgeek201@gmail.com> - 2013-02-20 12:53 -0800
  Re: Import Question Matteo Boscolo <matteo.boscolo@boscolini.eu> - 2013-02-20 22:01 +0100
  Re: Import Question Michael Herman <hermanmu@gmail.com> - 2013-02-20 13:03 -0800
  Re: Import Question Dave Angel <davea@davea.name> - 2013-02-20 16:23 -0500

csiph-web