Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to overwritten variables in python?

I am writing some python code to process huge amounts of data (almost 6 million pieces!). In the code, I'm using a huge for loop to process each set. In that loop, I'm using the same variables every loop and overwriting them. When I ran the program, I noticed that the longer I ran it, the slower it got. Furthermore, upon further experimenting, I discovered that the speed if you ran it for values 10,000 - 10,100 was the same as from 0 to 100. Thus I concluded that since I was not creating more variables and merely processing existing ones, every time I overwrote a variable, it must be being saved somewhere by python.

So: Am I right? must it be python saving my overwritten somewhere? Or am I wrong? Is something else happening?

like image 281
t3hn0081n470r Avatar asked Nov 12 '22 03:11

t3hn0081n470r


1 Answers

Python uses reference counting to keep track of variables. When all references to a variable are removed, that variable is garbage collected. However that garbage collection is done by python at it's own whim, not right away.

It could be that your code is going faster than python garbage collects, or that you have something wrong with your code. Since you didn't give any of your code there's no real way to know.

like image 105
korylprince Avatar answered Nov 14 '22 21:11

korylprince