Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here are my annotations for how I'm reading these. Feel free to correct or update:

1. Faster to use dict literals than the `dict()` constructor

2. Faster to sort a list in-place than to use `sorted()`

3. Faster to use multiple assignment than multiple individual assignments

4. Faster to evaluate multiple individual conditions than a single multi-comparison condition

5. Faster to use idiomatic implied truth value than to compare to `True`

6. Faster to use `!=` than `is not`

7. Fastest way to check for empty list `a` is the idiomatic `if not a`

8. Fastest way to check that an object is not null is `if a`

9. `enumerate` is faster than doing it yourself

10. Constructing strings with `+=` is faster than string surgery

11. Fastest way to place a number in a string is "%s" (not "%d")

12. For built-in types, using built-in functions (e.g. `len`) is faster than calling methods (e.g. `a.__len__`)

13. For built-in types, using built-in operators (e.g. `+`) is faster than calling methods (e.g. `a.__add__(b)`)

14. But for custom-defined classes, the opposite of #13 is true.

15. Adding up a bunch of numbers using `+` is faster than using `sum`

16. When initializing a large list, it is faster to use list comprehensions than to append each element individually

17. The fastest way to build a large dict is to use a dictionary comprehension

18. Same as #17

19. Not sure what the author is trying to demonstrate here. That `bool(a)` is faster when `a` is an empty list?

20. Faster to use implied truth value of an expression than to compare explicitly to `True` or `False`

21. The fastest way to refer to a list is to use the list literal (duh...)

22. It is faster to perform individual comparisons than to check for membership with `in`

23. Faster to `map` the `str` function to a list of values and join, than to manually build up the string. Also, when doing this, faster to use `xrange` than `range`

24. Faster to unpack than to access each list element individually. Also, as in #1, faster to use dict literal than the `dict()` constructor.

25. Faster to just return the boolean value of an expression than to check for it and then decide what to return.

26. Faster to give explicit values to keyword arguments than to expand a dict with kwargs.

27. To switch the values of `a` and `b`, it is faster to use a third variable than to do it in-place using unpacking.

28. Multiple assignment is faster than unpacking, which is faster than multiple individual assignments.

29. Fastest way to get integer part of a real is to use `math.floor`

30. Similar to #26 - it is faster to give explicit values to positional arguments than to expand a list of args.

31. Faster to access local vars than globals.

32. When trying to safely access a key in a dictionary, it is faster to use the idiomatic `D[k] if k in D` than to use the `get` method.



With PyPy: - `is` is very slightly faster - '%d' is slightly faster

Yet another caveat: the differences are often less that 1%, statistically not significant.


I'd expand on #9.

It's generally faster and more readable to use an idiomatic Python iteration than to use indexing inside the loop. The difference was small over 5 elements, but `enumerate(a)` will become dramatically faster than `range(len(a))` if you have a large list. A better comparison would be `enumerate` vs `xrange`.


I'd call any difference less than 1% to be equivalent. Even if it was statistically significant, it's not worth paying attention to.

My interpretation of many of these tests is that there isn't a difference.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: