I am looking at performance while joining strings from a mapping (Mapping[str, str]).
I have these two simple functions:
def nested(sequence, mapping):
string = ""
for x in sequence:
string += mapping[x]
return string
and
def flat(sequence, mapping):
return "".join(mapping[x] for x in sequence)
In my project, I found a clear loss in performance while using the "flat" solution. Initially, I choose that solution because: "Flat is better than nested.", but quickly moved to the "nested" solution.
I ran a timeit test with simpler sequences and mappings:
letters = (
"a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z"
)
mymapping = {x: x for x in letters}
mysequence = "mysequence"
print(timeit.timeit("nested(mysequence, mymapping)", globals=globals()))
print(timeit.timeit("flat(mysequence, mymapping)", globals=globals()))
and obtained:
In [683]: 0.675819274969399
0.965234256349504
This is 43% faster for the "nested" solution. I observed several times faster situations in my project.
In the docs, we can read: "The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence)". But I clearly see that "+" operator is faster in this configuration.
While I have no problem to stay with the "nested" solution:
Am I missing another "flat" solution? (no sum() possible with strings)
Do you have an explanation for this behavior?
Thanks.