Filter vs Compress

Posted by Afsal on 14-Oct-2022

Hi Pythonistas!

Today, we will check the performance change between Python's filter and compress methods. We know that both are used to filter iterable, the difference is in the filter we pass a function but for compress, we pass a selector sequence. Let us check with an example

Code

import itertools
import timeit

a = list(range(10))

filter_function = lambda x:x%2==0

def using_filter_method():
     return list(filter(filter_function, a))

selectors = [x%2== 0 for x in range(10)]


def using_compress_method():
    return list(itertools.compress(a, selectors))

time_for_filter = timeit.timeit(using_filter_method, number=1000_000)
print("Time for filter method", time_for_filter)

time_for_compress = timeit.timeit(using_compress_method, number=1000_000)
print("Time for compress method", time_for_compress)

Code

Time for filter method 0.7425476499993238
Time for compress method 0.2757011819994659

We can see that the compress method is much faster than the filter method. For smaller items, it is very easy to use compress over the filter, But for larger data selector sequence also becomes very larger which makes more memory usage and more difficult to manage due to the selector sequence.

Hope you have learned something from this post. Please share your valuable feedback with afsal@parseltongue.co.in