Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is `if x is None: pass` faster than `x is None` alone?

Timing results in Python 3.12 (and similar with 3.11 and 3.13 on different machines):

When x = None:
13.8 ns  x is None
10.1 ns  if x is None: pass

When x = True:
13.9 ns  x is None
11.1 ns  if x is None: pass

Python: 3.12.0 (main, Oct  7 2023, 10:42:35) [GCC 13.2.1 20230801]

How can doing more take less time?

Why is if x is None: pass faster, when it has to do the same x is None check and then additionally checks the truth value of the result (and does or skips the pass)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Times on other versions/machines:

  • Python 3.11: (12.4, 9.3) and (12.0, 8.8)
  • Python 3.13: (29.1, 24.6) and (29.1, 22.6)

Benchmark script (Attempt This Online!):

from timeit import repeat
import sys

for x in None, True:
    print(f'When {x = }:')
    for code in ['x is None', 'if x is None: pass'] * 2:
        t = min(repeat(code, f'{x=}', repeat=100))
        print(f'{t*1e3:4.1f} ns ', code)
    print()

print('Python:', sys.version)

>Solution :

Look at the disassembled code:

>>> import dis
>>> dis.dis('if x is None: pass')
  0           0 RESUME                   0

  1           2 LOAD_NAME                0 (x)
              4 POP_JUMP_IF_NOT_NONE     1 (to 8)
              6 RETURN_CONST             0 (None)
        >>    8 RETURN_CONST             0 (None)
>>> dis.dis('x is None')
  0           0 RESUME                   0

  1           2 LOAD_NAME                0 (x)
              4 LOAD_CONST               0 (None)
              6 IS_OP                    0
              8 RETURN_VALUE

The if case has a special POP_JUMP_IF_NOT_NONE operation, which is faster than a LOAD_CONST plus IS_OP. You can read the detailed discussion about it here: https://github.com/faster-cpython/ideas/discussions/154.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading