Optimization of importing modules in Python
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon
--
Chapters
00:00 Optimization Of Importing Modules In Python
01:09 Answer 1 Score 1
01:28 Answer 2 Score 1
01:49 Accepted Answer Score 6
02:35 Answer 4 Score 4
03:10 Thank you
--
Full question
https://stackoverflow.com/questions/5926...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #prematureoptimization
#avk47
ACCEPTED ANSWER
Score 6
The from collections import defaultdict and import collections should be outside the iterated timing loops, since you won't repeat doing them.
I guess that the from syntax has to do more work that the import syntax.
Using this test code:
#!/usr/bin/env python
import timeit
from collections import defaultdict
import collections
def first():
from collections import defaultdict
x = defaultdict(list)
def firstwithout():
x = defaultdict(list)
def second():
import collections
x = collections.defaultdict(list)
def secondwithout():
x = collections.defaultdict(list)
print "first with import",timeit.timeit('first()', 'from __main__ import first');
print "second with import",timeit.timeit('second()', 'from __main__ import second');
print "first without import",timeit.timeit('firstwithout()', 'from __main__ import firstwithout');
print "second without import",timeit.timeit('secondwithout()', 'from __main__ import secondwithout');
I get results:
first with import 1.61359190941
second with import 1.02904295921
first without import 0.344709157944
second without import 0.449721097946
Which shows how much the repeated imports cost.
ANSWER 2
Score 4
I'll get also similar ratios between first(.) and second(.), only difference is that the timings are in microsecond level.
I don't think that your timings measure anything useful. Try to figure out better test cases!
Update:
FWIW, here is some tests to support David Beazley's point.
import math
from math import sqrt
def first(n= 1000):
for k in xrange(n):
x= math.sqrt(9)
def second(n= 1000):
for k in xrange(n):
x= sqrt(9)
In []: %timeit first()
1000 loops, best of 3: 266 us per loop
In [: %timeit second()
1000 loops, best of 3: 221 us per loop
In []: 266./ 221
Out[]: 1.2036199095022624
So first() is some 20% slower than second().
ANSWER 3
Score 1
first() doesn't save anything, since the module must still be accessed in order to import the name.
Also, you don't give your timing methodology but given the function names it seems that first() performs the initial import, which is always longer than subsequent imports since the module must be compiled and executed.
ANSWER 4
Score 1
My guess, your test is biased and the second implementation gains from the first one already having loaded the module, or just from having it loaded recently.
How many times did you try it? Did you switch up the order, etc..