Timeline for Insert str into larger str in the most pythonic way
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 11, 2019 at 15:36 | comment | added | scnerd | While I appreciate profiling answers when they contribute useful information, this doesn't seem to address either the question asked ("what is the most pythonic way") nor the general goal of CodeReview, which is to provide holistic suggestions/critique for working code. This simply enumerates a bunch of ways to concatenate strings and demonstrates that, unless you're doing this a lot, it doesn't really matter how you do it, except that the OP's original code is plenty clean and efficient. | |
Sep 11, 2019 at 12:37 | comment | added | GZ0 |
My bad. I had a wrong impression about reversed .
|
|
Sep 11, 2019 at 12:33 | comment | added | Reinderien |
@GZ0 I'm glad that you did some performance analysis and updated your answer. Note that the tuple call is not redundant because reversed cannot accept an iterator.
|
|
Sep 11, 2019 at 10:15 | comment | added | GZ0 | I have added my version of performance tests in my post. I tried my best to optimize each method to achieve its best performance. For some methods, several variations are implemented for comparison. | |
Sep 11, 2019 at 3:50 | comment | added | GZ0 |
Meanwhile, the slowness of insertion is due to all the insertions happening at the beginning of the list. The implementation would not be so awkward if not because of the decending order.
|
|
Sep 11, 2019 at 3:28 | comment | added | Reinderien |
@GZ0 The alternatives - probably adding a sort() somewhere, to be more robust - are probably going to incur more cost than assuming descending order. Iterating in reverse order is efficient, especially with indexes. Only one of the methods calls reversed , and it's difficult to say whether that's the limiting factor in speed of that method.
|
|
Sep 11, 2019 at 3:25 | comment | added | GZ0 |
One problem of the tests is that it assumes that offsets is in descending order, which adds unnecessary overhead to all the str.join implementations here.
|
|
Sep 11, 2019 at 2:24 | history | answered | Reinderien | CC BY-SA 4.0 |