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 script, call import gc to load the library. Call gc.collect() to clear up excess space that is not being used. When you perform several transformations, functions, and copies in the data, references, values, and indices running behind the code begin to pile up, so it is helpful to call it every so often.

Use Generators

Generators are similar to functions, but iterate and returns one item at time, instead of all at once. If you have a large dataset or list, using generators allows you to utilize what you have so far, instead of being forced to wait until the entire dataset is accessible.

To create a generator, construct a function how you normally would, but substitute the return keyword with yield. Using the next operator will return the next iteration of the generator.

#Defining and creating instance of generator
	def gen_even_nums(max):
	    num = 0
	    while num < max:
	        yield n
	        n += 2
	even_nums = gen_even_nums(100)

	#To get values from the generator
	next(even_nums) #[Output]: 0
	next(even_nums) #[Output]: 2
	next(even_nums) #[Output]: 4

Generators are much more computationally efficient than functions because they do not need to store all the results within the memory, but generate them on the spot; hence, a small amount of memory is only being used when we request for a result. As an additional bonus, using generators is a great way to clean up code and avoiding messy iterators.

#computer-science #hacking #data-science #programming #coding

5 Quick & Easy Hacks to Write More Computationally Efficient Code
1.10 GEEK