Code optimizations #1924
-
How are code optimizations received usually? For example, is_success can be made ~2x as fast if you avoid making two comparisons by using integer division. For simple functions (with decent names/docstrings) I believe the loss of readability that usually comes with an optimization is acceptable. Thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think the only sensible approach is case-by-case. Generally I'd optimise for readability over micro-optimisation, but for the specific case perhaps? they're two equally decent formulations... @classmethod
def is_success(cls, value: int) -> bool:
"""
Returns `True` for 2xx status codes, `False` otherwise.
"""
return 200 <= value <= 299 vs. @classmethod
def is_success(cls, value: int) -> bool:
"""
Returns `True` for 2xx status codes, `False` otherwise.
"""
return value // 100 == 2 Writing this now, and looking at them side by side I guess I'd tend to suggest just sticking with the first. But there's no specific rule we can apply when looking at that trade-off. |
Beta Was this translation helpful? Give feedback.
I think the only sensible approach is case-by-case.
Generally I'd optimise for readability over micro-optimisation, but for the specific case perhaps? they're two equally decent formulations...
vs.
Writing this now, and looking at them side by side I guess I'd tend to suggest just sticking with the first. But there's no specifi…