I want to create a long string from a list of objects that contain smaller strings. A simplified example is a chat log:
class Line:
def __init__(self, user, msg):
self.user = user
self.msg = msg
Now I try to create a log:
log = []
for line in lines:
log.append("{0}: {1}".format(log.user, log.msg))
log_str = "\n".join(log)
On a fast machine I get only around 50000 lines per second (according to tqdm
).
Alternatives I tried are just concatenating the string:
log.append(log.user + ": " + log.msg + "\n")
or directly appending it to log_str
and both are slower.
As far as I know, concatenation is faster with "\n".join(string_list)
, but how can I speed up creating the lines?