Hacker News new | past | comments | ask | show | jobs | submit login
Python Performance: Why 'if not list' is 2x Faster Than Using len() (codingconfessions.com)
3 points by sohkamyung 7 hours ago | hide | past | favorite | 2 comments





> But this isn’t just about coding style, one of these is ~2x faster than the other, see for yourself:

FWIW, it's not necessary to start an interpreter for this test.

  $ py3.13 -m timeit --setup "l = []" "not l"
  10000000 loops, best of 5: 20.7 nsec per loop
  $ py3.13 -m timeit --setup "l = []" "len(l) == 0"
  10000000 loops, best of 5: 31 nsec per loop

The explanation is paywalled.

> This post is for paid subscribers.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: