Python using NaN or None as sentinel

Comparing to None instead of NaN is over 50 times faster in Python.

speed improvement comparing to None vs. nan in Python


Python 3.6.1 IPython 6.1.0 Numpy 1.12.1

from numpy import isnan
%timeit ~isnan(0)

1000000 loops, best of 3: 1.07 ┬Ás per loop

%timeit 0 is not None

10000000 loops, best of 3: 16.5 ns per loop

Leave a Comment