> Actually there's no need to optimize the |= operator -- for strings we
have to optimize += *because* strings are immutable, but for dicts we would
define |= as essentially an alias for .update(), just like the relationship
between += and .extend() for lists, and then no unnecessary objects would
be created.

Yeah that's why I noted that any form of optimization for the |= operator
on dicts would not be the same as += is for strings. I wasn't actually sure
of what form any potential optimization would take for the |= operator
though. What exactly was the performance question/point in reference to?
The question seemed to imply that there would be some minor performance
detriment from using |=, but it's not clear to me as to when that would be
a factor.

> ## What about performance?

> Performance is not the only objective when using Python. Switching to
inplace operators (here |=) is a generally useful and well-known technique
(it also applies to string and list concatenation, for example).

Also with lists, I recall that using the += operator is *very slightly*
faster than list.extend() in most situations:

>>> ls_plus_eq = """\
for i in range(1_000):
     ls += [x for x in range(10)]
"""
>>> ls_extend = """\
for i in range(1_000):
     ls.extend([x for x in range(10)])
"""
>>> timeit.timeit(ls_plus_eq, setup="ls = []", number=10_000)
6.563132778996078
>>> timeit.timeit(ls_extend, setup="ls = []", number=10_000)
6.695127692000824
>>> timeit.timeit("ls+=other", setup="ls = []; other=[i for i in
range(100_000)]", number=10_000)
4.400735091003298
>>> timeit.timeit("ls.extend(other)", setup="ls = []; other=[i for i in
range(100_000)]", number=10_000)
4.574331789997814
>>> timeit.timeit("ls+=other", setup="ls = []; other=[i for i in
range(100)]", number=10_000_000)
3.5332175369985634
>>> timeit.timeit("ls.extend(other)", setup="ls = []; other=[i for i in
range(100)]", number=10_000_000)
3.7756526679950184

(Python 3.8)

It seems to be a difference of only ~2-4% in most cases (~6-7% with the
last set), but I find it interesting that += is barely faster. Of course,
some of the above examples are fairly unrealistic, for most practical use
cases they're essentially the same. I tend to prefer ls.extend() most of
the time myself (the behavior is a bit more obvious). I'm mostly just
curious if the difference between |= and dict.update() would end up being
similar as far as performance goes, with |= having a negligible advantage
over dict.update() in most situations.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/243KH67NWORPWGR75D3ZWRNGKUPRWG5F/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to