Python using NaN or None as sentinel

1 minute read

Comparing to None instead of NaN is:

  • 10-100 times faster in CPython
  • more than 1000 times faster in PyPy3

Benchmarks

Tests using Intel Haswell CPU and

ipython

Python 3.7.0 IPython 7.0.1 Numpy 1.15.2

pypy3-ipython

Python 3.5.3 (PyPy3 6.0) IPython 7.0.1 Numpy 1.15.2

Numpy NaN

Numpy is well known to be slower at scalar operations than pure Python. But many data science and STEM application using arrays are vastly faster and more convenient with Numpy than pure Python methods.

from numpy import isnan
%timeit ~isnan(0)
  • CPython: 1.5 µs ± 39.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
  • PyPy3: 8.06 µs ± 226 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Python NaN

from math import isnan
%timeit ~isnan(0)
  • CPython: 95.4 ns ± 5.84 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
  • PyPy3: 2.1 ns ± 0.0488 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

Python None

%timeit 0 is not None
  • CPython: 17.5 ns ± 0.202 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
  • PyPy3: 2.08 ns ± 0.0204 ns per loop (mean ± std. dev. of 7 runs, 1000000000 loops each)

Leave a comment