filter() is 30 times slower than manually filtering
edit: more benchmarks
from time import perf_counter
from random import getrandbits
import gc; gc.disable()
foo = lambda x: x
a = [getrandbits(1) for _ in range(1000)]
t = perf_counter()
for _ in range(10**5):
b=[*filter(foo, a)]
print(perf_counter()-t) # PyPy: 11.15 secs | CPython: 3.91 secs
t = perf_counter()
for _ in range(10**5):
b=[e for e in a if foo(e)]
print(perf_counter()-t) # PyPy: 0.46 secs | CPython: 5.27 secs
t = perf_counter()
for _ in range(10**5):
b=[]
for e in a:
if foo(e): b.append(e)
print(perf_counter()-t) # PyPy: 0.40 secs | CPython: 8.06 secs
t = perf_counter()
for _ in range(10**5):
b=[*filter(None, a)]
print(perf_counter()-t) # PyPy: 2.87 secs | CPython: 0.96 secs
t = perf_counter()
for _ in range(10**5):
b=[e for e in a if e]
print(perf_counter()-t) # PyPy: 0.34 secs | CPython: 1.81 secs
t = perf_counter()
for _ in range(10**5):
b=[]
for e in a:
if e: b.append(e)
print(perf_counter()-t) # PyPy: 0.34 secs | CPython: 4.46 secs