On Thu, Apr 02, 2020 at 03:37:34PM +1100, Chris Angelico wrote: > On Thu, Apr 2, 2020 at 3:27 PM Steven D'Aprano <st...@pearwood.info> wrote: > > Or maybe that's just an argument that no solution is going to solve > > *every* problem. What do we do about people who write this: > > > > buf = f'{buf}{substring}' > > > > inside a loop? We can't fix everyone's code with one change. > > > > I don't know whether your point was that this is bad code and can't be > optimized, or that it's good code but still can't be optimized by this > proposal.
Neither. It's that anyone building a string like that isn't the target of this proposal. I could have given various examples, I just happened to pick f-string: buf = '%s%s' % (buf, substring) buf = '{}{}'.format(buf, substring) buf = ''.join(itertools.chain(buf, substring)) Put any of them into a loop, and they are likely to be exceedingly slow for large N. But fixing them is not part of Paul's proposal. > But if the former, then I put it to you that this isn't > actually bad code. Repeated string concatenation doesn't suddenly become efficient just because you wave a magic f-string at it. That appends substring to the buffer each time through the loop, giving quadratic performance. On my computer, appending a single character 'a' to the buffer each time, I get: 10_000 loops, 0.4 seconds (actual time) 50_000 loops, 9.8 seconds # expect 5*0.4 = 2 seconds 100_000 loops 39 seconds # expect 10*0.4 = 4 seconds 200_000 loops 160 seconds # expect 20*0.4 = 8 seconds The expected times are assuming that the time is proportional to the number of elements, i.e. O(N). If we assume O(N**2) then the expected times would be 10, 40 and 160 seconds, so this is a textbook example of quadratic slowdown. (At least on my computer.) -- Steven _______________________________________________ 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/Q62OEAVPMMZPXI2HPSPYGPOUOSBTYXJE/ Code of Conduct: http://python.org/psf/codeofconduct/