[SciPy-user] What can be improved ?
Steve Schmerler
elcorto at gmx.net
Thu May 17 15:30:55 EDT 2007
Anne Archibald wrote:
> On 17/05/07, Steve Schmerler <elcorto at gmx.net> wrote:
>
>> In most cases, import * results in poorly readable code. import * inside functions
>> and more general importing *anything* in functions isn't a good idea because: usually
>> you write a function in order to execute a chunk of code more than once. So if you
>> call the func N times, all the importing will also be done N times which is simply
>> overhead (execution time + memory action). A simple-minded timing example is
>> attached, which shows the effect clearly (although not utilizing some nifty tricks
>> offerd by the timeit module). Running it (from Ipython or the cmd line), I see the
>> warning David mentioned. Maybe this was intruduced with Python 2.4.x and you are on
>> 2.3.x if you don't see it?
>
> A technical point: python imports are implemented to do nothing on
> re-import, so calling import many times is not actually a problem.
Argh, yes. I was (partly) aware of that. Should have analysed my test times in more
detail :) To be honest, I'm not a total expert in those nitfy technial details.
Changing this test script of mine to compare (1) import * in a function, (2) import
module in a function and (3) import nothing in a function, I get
from_mod_import_all:
dt: [ 1.32939887 0.31759715 0.32567406 0.32297516 0.32898188 0.33600211
0.33455801 0.32793999 0.3268559 0.32574892]
min: 0.317597150803
max: 1.32939887047
mean: 0.427573204041
-----------------------
import_mod:
dt: [ 0.05784607 0.05776787 0.0569551 0.05641389 0.05731797 0.0634551
0.06005907 0.05657482 0.05893087 0.05673099]
min: 0.0564138889313
max: 0.0634551048279
mean: 0.0582051753998
-----------------------
no_import:
dt: [ 0.05400395 0.05075312 0.051965 0.05418086 0.050946 0.05176306
0.05257511 0.05177307 0.05184007 0.05447507]
min: 0.0507531166077
max: 0.054475069046
mean: 0.0524275302887
So, in the from_mod_import_all case, the first "from scipy import *" is slow, the
others are faster (no re-import?), but are actually much slower than in the
import_mod and no_import case. Hmm, why is that so? Or am I doing something wrong...
--
cheers,
steve
I love deadlines. I like the whooshing sound they make as they fly by. -- Douglas Adams
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 1568 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20070517/a64134f9/attachment.py>
More information about the SciPy-User
mailing list