Difference between memory usage when importing modules
Asked Answered
H

1

6

I'd like to know whats is the difference between the memory usage when importing modules in these ways:

import Mod1
from Mod1 import *
from Mod1 import a,b,c

Mainly between the first two.

Hophead answered 15/4, 2015 at 14:35 Comment(4)
You can find an answer here: #7373466Holmes
I didn't understand well... So the first two examples, there isn't difference between them?Alexandra
@HugoSousa The op here is asking to compare the memory usage of different import methods, the link is answering the memory usage of imports in general.Priestcraft
Yes, there is differences: in the first/second one you are importing everything inside the module; in the third one you are only importing a, b and c function. The difference between the first one and the second one is in the first it is not a module here, it's a package. Using the first import you have to do Mod1.function(), in the second import you can call the function directly.Holmes
E
9

The first uses the least memory since it only creates a single name in the module scope.

The second uses the most (assuming Mod1 contains more than just a, b, and c either explicitly or in __all__) since all names are recreated.

In all three cases the entire module is imported and executed, so if you're looking for large memory savings this is not what you want to optimize.

Episodic answered 15/4, 2015 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.