bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Python list micro-benchmarks


From: Bruno Haible
Subject: Re: Python list micro-benchmarks
Date: Mon, 08 Apr 2024 16:23:41 +0200

Hi Collin,

> I think that I see the issue here. I believe this is a case of the
> "prior knowledge summarization engine" being incorrect. Given this
> statement:
> 
>     a += [var]
> 
> it seems to think that a new copy of 'a' is being created. This is
> incorrect. The given statement is equal to:
> 
>    operator.iadd(a, [var])
> 
> The Python documentation states that "For mutable targets such as
> lists and dictionaries, the in-place method will perform the update,
> so no subsequent assignment is necessary [2]."

OK, that explains it:
  - It explains why the '+= [item]' was nearly constant-time even on a
    list of length 1000.
  - It explains ChatGPT's failure: Probably there are more explanations
    regarding += on strings, on the web, than regarding += on lists.
    So ChatGPT used the "common" explanation, for strings, and then
    substituted s/string/list/.

Thanks. Now I agree to converging on .append(..), because by my common
measure "what is surprising to a naïve developer?" the '+= [item]'
syntax has two negative scores:
  - It's surprising that += is optimized differently than +.
  - It's surprising that lists are optimized differently than strings.

Bruno






reply via email to

[Prev in Thread] Current Thread [Next in Thread]