DEV Community

Discussion on: SpeedUp Python List and Dictionary

Collapse
 
idanatias profile image
Idan Atias • Edited

Thanks for sharing.
I wonder though if this reduces code readability.
If we are not talking about mission critical paths, then it may not worth the 10-15 % reduction.

Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

I wonder though if this reduces code readability.

Yes it does - idiomatic Python code is typically not written this way.

If we are not talking about mission critical paths, then it may not worth the 10-15 % reduction.

This. Before applying this technique, make sure to measure the performance of your entire application!

Finally, a better advice for beginners (in my opinion), is to advise them to use comprehensions when they can:

new_list = [append(num) for num in list]

is more idiomatic and even faster than the technique presented here.

Collapse
 
sharmapacific profile image
Prashant Sharma

@Dimitri Did you tried this -

Kindly Look at the result below -

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

append_outside_loop(list(range(1, 9999999)))
o/p - function - append_outside_loop, took 445 ms to complete

and as you said -

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

append_outside_loop(list(range(1, 9999999)))
o/p - function - append_outside_loop, took 602 ms to complete
Thread Thread
 
dmerejkowsky profile image
Dimitri Merejkowsky

Excellent! You took the time to verify what I was saying without giving proof - and you we right to do so!

The problem is that the last line in the second function is actually building a list.

Here's a better example:

def is_even(x):
    return x % 2 == 0

@timeit
def func1(my_list):
   """ calling append() in a loop """
    my_other_list = []
    for x in my_list:
        if is_even(x):
            my_other_list.append(x)


func1(list(range(1, 9999999)))

@timeit
def func2(my_list):
    """Saving one 'op' by not looking up the is_even 
   function every time
    """
    my_other_list = []
    func = is_even
    for x in my_list:
        if func(x):
            my_other_list.append(x)


func2(list(range(1, 9999999)))

@timeit
def func3(my_list):
    """Using list comprehensions
    """
    my_other_list = [x for x in my_list if is_even(x)]

func3(list(range(1, 9999999)))