Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there memory efficiencies gained when code is wrapped in functions?

I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to create functions.

I have just noticed that creating functions and calling them seems to be much more efficient than writing lines of code and deleting containers as I am finished with them.

for example:

def someFunction(aList):
    do things to aList
    that create a dictionary
    return aDict

seems to release more memory at the end than

>>do things to alist
>>that create a dictionary
>>del(aList)

Is this expected behavior?

EDIT added example code

When this function finishes running the PF Usage shows an increase of about 100 mb the filingsList has about 8 million lines.

def getAllCIKS(filingList):
    cikDICT=defaultdict(int)
    for filing in filingList:
        if filing.startswith('.'):
            del(filing)
            continue
        cik=filing.split('^')[0].strip()
        cikDICT[cik]+=1
        del(filing)
    ciklist=cikDICT.keys()
    ciklist.sort()
return ciklist

allCIKS=getAllCIKS(open(r'c:\filinglist.txt').readlines())

If I run this instead I show an increase of almost 400 mb

cikDICT=defaultdict(int)
for filing in open(r'c:\filinglist.txt').readlines():
    if filing.startswith('.'):
        del(filing)
        continue
    cik=filing.split('^')[0].strip()
    cikDICT[cik]+=1
    del(filing)

ciklist=cikDICT.keys()
ciklist.sort()
del(cikDICT)

EDIT I have been playing around with this some more today. My observation and question should be refined a bit since my focus has been on the PF Usage. Unfortunately I can only poke at this between my other tasks. However I am starting to wonder about references versus copies. If I create a dictionary from a list does the dictionary container hold a copy of the values that came from the list or do they hold references to the values in the list? My bet is that the values are copied instead of referenced.

Another thing I noticed is that items in the GC list were items from containers that were deleted. Does that make sense? Soo I have a list and suppose each of the items in the list was [(aTuple),anInteger,[another list]]. When I started learning about how to manipulate the gc objects and inspect them I found those objects in the gc even though the list had been forcefully deleted and even though I passed the 0,1 & 2 value to the method that I don't remember to try to still delete them.

I appreciate the insights people have been sharing. Unfortunately I am always interested in figuring out how things work under the hood.

like image 926
PyNEwbie Avatar asked Nov 18 '25 02:11

PyNEwbie


2 Answers

Maybe you used some local variables in your function, which are implicitly released by reference counting at the end of the function, while they are not released at the end of your code segment?

like image 187
J S Avatar answered Nov 19 '25 15:11

J S


You can use the Python garbage collector interface provided to more closely examine what (if anything) is being left around in the second case. Specifically, you may want to check out gc.get_objects() to see what is left uncollected, or gc.garbage to see if you have any reference cycles.

like image 35
Joe Avatar answered Nov 19 '25 17:11

Joe