Efficient code- not a black box anymore!

Python, preferably the most flexible language for everyone who wants to complete the task with just simple, readable lines of code. But only a few spend a moment to think if the code is faster enough, is my code consuming so much temporary storage, how much I have optimized in execution time?

Photo by Juan Rumimpunu on Unsplash

If we step back and take a moment to replace few practices with the case-specific features that python provides, we will visibly see changes in execution and we will start building a map on replacing (looping with temp variable in list ==> list comprehension). Keep in mind that the number of lines of code is not always the only factor that determines efficient code. In my perspective, no one can write fully efficient code in the first hand of execution, its always giving a moment to think what builtin methods, modules we can use such that memory consumption is less, execution time becomes faster for the specific use case.

There are always cases on non-pythonic and pythonic way, though apart from the verbose factor, no visible changes can be seen in accomplishing the task, in the long run, the Pythonic way considers efficiency(less memory, faster execution)

Lets now consider the better and best approach in handling lists:

Now am hearing you asking, though the second one looks less verbose, but how do you say it’s faster in execution. Don’t worry, will cover it :) Let’s first see the pythonic approaches that a programmer can start replacing the most known practices, and later we can prove how efficient these slight changes can lead to.

Let’s now look at the components python offers to help make the code more efficient.

range() function:

Many of you may know the range() function to create a sequence or to iterate through some iterables with a given start, stop. range(start, stop, increment)

But in actual, range() returns a <class ‘range’>(range object) which we can unpack it with the star(*) operator to the types we needed without explicitly calling out the methods like list(), set()

nums_list = [*range(1,6,2)]

print(nums_list) o/p: [1,3,5]

enumerate() function:

This function can be used to pair up the values with already existing values in an iterable without explicitly appending index variables to a list. Use unpacking on the enumerate object.

If we wanted to apply some formatting to all the elements in the list, then looping through the list and then formatting each item is the only solution that comes to mind. That may perform the task, but as I said always take a moment to think what builtin can be used to perform the execution faster.

map(method, object) function — will apply the method to each item in the object. As you know pretty much by now, use unpacking on the map object.

Numpy arrays:

Memory efficient and faster execution when compared to python lists. When dealing with data stored in large lists, working on the data with Numpy arrays could be a good alternative. To make sure the data comes in preserves homogeneity -NumPy arrays can be used.

One of the very best features is broadcasting which is not present in lists.

Suppose you want to double the numbers in the list, then you may end up using list comprehension for squaring thinking you have made it less verbose. But NumPy comes in rescue for such broadcasting.

Numpy supports boolean indexing so it's easy to retrieve the values based on a condition without having to loop each item, check for a condition, or a list comprehension.

How we can use several other modules that python provides such that we visibly know the execution time and memory consumption is improved. There are methods available to perform the runtime examining, memory consumption check for several lines of code, which will cover those in the upcoming series.

Efficiency is not about writing advanced features that python supports, or reducing the lines of code to very minimal, it is the right choice in using the features python provides based on the use case. Choose the path for your case on proven factors, not by the number of lines of code.

Photo by Oliver Roos on Unsplash

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store