relative speed of incremention syntaxes (or "i=i+1" VS "i+=1")

Terry Reedy tjreedy at udel.edu
Sun Aug 21 22:15:07 EDT 2011


On 8/21/2011 7:49 PM, Laurent Payot wrote:
> I made Python my language of choice because of its readability and
> simpleness, and not because of its speed. But it's always good to
> know what is the fastest sometimes when you don't want to write a
> module in C. So I was just wondering if there was a difference. There
> is, of a few percent. Anyway I will keep on using the 2% slower "i +=
> 1" because for me that's less prone to errors because you write the
> variable only once, and that's more important than speed.

For longer variable names, it is also easier and faster to read once one 
gets used to the idiom.

number_of_chars += 1 # versus
number_of_chars = number_of_chars + 1

Not repeating was a major reason for the addition.

-- 
Terry Jan Reedy




More information about the Python-list mailing list