Today, we will discuss optimization techniques in Python. In this article, you’ll learn how to speed up your code by avoiding recalculation in lists and dictionaries.

Let’s first write a decorator function to calculate the execution time of the function, which is convenient to test the speed of different code:

import functools
import time

def timeit(func):
    @functools.wraps(func)
    def newfunc(*args, **kwargs):
        startTime = time.time()
        func(*args, **kwargs)
        elapsedTime = time.time() - startTime
        print('function - {}, took {} ms to complete'.format(func.__name__, int(elapsedTime * 1000)))
    return newfunc
Copy the code

Avoid reevaluating in lists

Within the loop

Code:

@timeit
def append_inside_loop(limit):
    nums = []
    for num in limit:
        nums.append(num)

append_inside_loop(list(range(1, 9999999)))
Copy the code

In the above function. Append recalculates the function reference each time through the loop. Total time taken by the above function after execution:

o/p - function - append_inside_loop, took 529 ms to complete
Copy the code

Outside the loop

Code:

@timeit
def append_outside_loop(limit):
    nums = []
    append = nums.append
    for num in limit:
        append(num)

append_outside_loop(list(range(1, 9999999)))
Copy the code

In the above function, we evaluate nums.append outside the loop and use append as a variable inside the loop. Total time:

o/p - function - append_outside_loop, took 328 ms to complete
Copy the code

As you can see, when we append to a local variable outside the for loop, this takes much less time and can speed up the code by 201 ms.

Avoid reevaluating in the dictionary

Inside the loop

Code:

@timeit
def inside_evaluation(limit):
    data = {}
    for num in limit:
        data[num] = data.get(num, 0) + 1

inside_evaluation(list(range(1, 9999999)))
Copy the code

Total time taken by the above function:

o/p - function - inside_evaluation, took 1400 ms to complete
Copy the code

Outside the loop

Code:

@timeit
def outside_evaluation(limit):
    data = {}
    get = data.get
    for num in limit:
        data[num] = get(num, 0) + 1


outside_evaluation(list(range(1, 9999999)))
Copy the code

Total time taken by the above function:

o/p - function - outside_evaluation, took 1189 ms to complete
Copy the code

As you can see, our code here is 211 milliseconds faster.

英文原文 :

Dev. To/sharmapacif…

This article is only for learning, copyright belongs to the original author, if there is infringement, please contact delete.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials

Follow the Python circle and get good articles delivered daily.