Source: Pixabay.

5 Quick & Easy Hacks to Write More Computationally Efficient Code

Transform your inefficient coding style

Andre Ye
Published in
5 min readJun 23, 2020

--

When working as a computer scientist — be it with performing costly big data manipulations in data science or running expensive simulations in game development — we all tend to be on the liberal side in terms of computational usage. However, if you’re not careful about using computational space, your development will be a nightmare. Here are 5 quick and easy ways you can spare yourself that experience by writing more computational efficient code.

Freeing up memory

Whenever you create any variable, even if you are not using it anymore, it exists in the python environment, is taking up space, and does not free on its own. If you load a massive DataFrame using Pandas, make a copy or edit of it, and never use it again, the original DataFrame will still be in the RAM, consuming memory.

After you decide an object is not necessary anymore — say, for instance, a DataFrame that contains columns not needed for analysis and to be discarded — explicitly delete it using del object_name. Even after deleting it, however, there will be some remaining memory usage.

The garbage collection module in Python is very helpful with freeing up used memory. At the beginning of your…

--

--