Code optimizations #1924
Answered
by
lovelydinosaur
omgitsaheadcrab
asked this question in
General
|
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? |
Answered by
lovelydinosaur
Nov 2, 2021
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 <= 299vs. @classmethod
def is_success(cls, value: int) -> bool:
"""
Returns `True` for 2xx status codes, `False` otherwise.
"""
return value // 100 == 2Writing 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. |
0 replies
Answer selected by
StephenBrown2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to 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...
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…