how to avoid checking the same condition repeatedly ?

Chris Angelico rosuav at gmail.com
Tue Oct 29 02:39:04 EDT 2013


On Tue, Oct 29, 2013 at 5:14 PM, Peter Cacioppi
<peter.cacioppi at gmail.com> wrote:
> Nobody (yes, his name is Nobody) said:
>
> "If you're sufficiently concerned about performance that you're willing to
> trade clarity for it, you shouldn't be using Python in the first place."
>
> I don't think it's correct to imply that people very concerned about performance should not use Python. (And I agree, Nobody implied that ;)

No, I don't think he implied that. You can care about performance
while still putting code clarity as a higher priority :) If you
actually profile and find that something-or-other is a bottleneck,
chances are you can break it out into a function with minimal loss of
clarity, and then reimplement that function in C (maybe wielding
Cython for the main work). That doesn't compromise clarity.
Duplicating a loop to hoist a condition _does_. Of course, in C, you
can let the compiler do it for you, but Python can't be sure that it's
as constant as you think, so it can't be changed.

ChrisA



More information about the Python-list mailing list