memory overhead using from-import?

Bengt Richter bokr at oz.net
Sun Dec 21 22:13:45 EST 2003


On Sun, 21 Dec 2003 15:10:34 +0000, William Trenker <wtrenker at hotmail.com> wrote:

>Erik Max Francis wrote:
>
>> Consider that the objects created in a module can be highly
>> interdependent on each other
>
>Yes, you are right.  Usually it makes no sense to import one "piece" of
>a module because of the inter-connectedness.
>
>> The bigger question here is why you think you really should be
>> avoiding the full importation of the module.
>
>In this case, the "piece" is totally independent of anything else in the
>module.  And this piece (cgi.escape) is a very small function.  I was
>hoping to "lift" just this one, small independent function since it's
>functionality is quite common.

>
>> The overhead involved in an interpreted language like
>> Python for importing the cgi module and only using one function inside
>> it vs. avoiding the importation at all is extremely minimal.
>
>The cgi module adds 1.46 megabytes to the virtual memory size of the
>process into which it is imported.  That seems excessive considering I
>only want access to a small, independent function.
Note that secondary imports will "stick" also, and cgi.py has a few ;-)

[19:10] /d/Python23/Lib>grep import cgi.py
import sys
import os
import urllib
import mimetools
import rfc822
import UserDict
from StringIO import StringIO
        import tempfile
    import traceback
    import re
# Call test() when this file is run as a script (not imported as a module)

(BTW, it's nice to have msys on win32 and be able to use a real grep, in case you were wondering ;-)

What I did in the other post did not eliminate these secondary imports,
sorry to raise hopes (some kind of pun there ;-) I think to extract an
"independent" function or class etc. without all the entanglements would
require source processing, and extracting only what was necessary to support
the thing to be extracted.
  
>
>Yes, on today's gigabyte computers a megabyte isn't worth worring about.
> But on old systems and on embedded processors, the memory size can be
>important.
>
Stuff expands or multiplies to use available resources. Smaller stuff, more stuff ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list