Skip to main content
  1. About
  2. For Teams
14 events
when toggle format what by license comment
Feb 14, 2023 at 16:50 answer added JaMa timeline score: 2
Feb 2, 2023 at 17:11 comment added JonSG @HelpfulHelper your assumption is incorrect. join() is by far the fasted method. Feel free to try the code below.
Feb 2, 2023 at 17:10 comment added HelpfulHelper @JonSG yes, join() is slower because it does more operations
Feb 1, 2023 at 21:54 vote accept Étienne
Feb 1, 2023 at 21:37 comment added Étienne @chepner s2 indeed uses += and is getting the optimization. This is fully intentional, since s2 benchmarks the optimization. s1 is the completely unoptimized case.
Feb 1, 2023 at 17:00 comment added JonSG @HelpfulHelper Are you suggesting we should expect join() to be slow as a result of extra operations?
Feb 1, 2023 at 16:43 comment added JNevill I think it's worth mentioning that the PEP recommendation isn't to ensure you have the fastest code, but rather that your code doesn't experience non-linear timing growths depending on which implementation (cpython, jython, etc) you use. It's a middle ground between non-linear timings across implementations that may lead to unexpected slow performance, and the fastest possible execution because of a particular implementations efficiencies.
Feb 1, 2023 at 16:38 comment added HelpfulHelper join internally does something like arg[0]+<value>+arg[1]+<value>+.... So instead of joining 3 items together like with a+b+c, 5 items are joined together, like a+''+b+''+c, therefore, when using join instead of the + operator, it internally does len(arg)-1 additional calculations. When doing a single operation, it doesn't really matter, but when doing 100000 iterations like in your program, 200000 additional operations are done, and it makes a difference.
Feb 1, 2023 at 16:37 comment added chepner s2 uses +=, so you are still getting the optimization. The idea with join is not to call it many times with small lists, but to build up one list and pass it to one call of join.
Feb 1, 2023 at 16:37 comment added Friedrich Replying to my own question: on my machine, list creation accounted for about 15% of the time. Not enough to explain the differences away.
Feb 1, 2023 at 16:35 comment added Barmar BTW, you should look into using the timeit module for benchmarking.
Feb 1, 2023 at 16:34 answer added JonSG timeline score: 7
Feb 1, 2023 at 16:28 comment added Friedrich How expensive is it to create [s3, str(i), '3'] alone?
Feb 1, 2023 at 16:22 history asked Étienne CC BY-SA 4.0
Morty Proxy This is a proxified and sanitized view of the page, visit original site.