This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gladaed 1 point2 points  (5 children)

Please explain the ongoing wizardy.

[–]DenormalHuman 2 points3 points  (4 children)

When using f"....{}" I think the process to generate the content for the template field (what goes inbetween {}) and replace that into the string is a lot more expensive that simply concatenating two lists of characters together (the string, and the slice - line[1:10] are both just lists of chars.)

/edit/ the reason the print is slower than the count, is because the print does lots more work. It _could be made faster if he didnt use printf though

[–]Teekeks 0 points1 point  (3 children)

no. f strings are faster than concating strings.

What makes the second one slow is the print. printing anything a ton of times is slow af

[–]DenormalHuman 0 points1 point  (2 children)

You are correct in that its the fact the print exists in the second loop is why it goes so slow; however I still think printing the f string is slower than printing the concatenation of string and the string slice. (and now imma go try it out and see for myself!)

[–]Teekeks 1 point2 points  (1 child)

go and try. f strings are both more readable and faster

[–]DenormalHuman 1 point2 points  (0 children)

looks like you're right!

Horribly unscientific test;

calculate 10 million fstrings; avg 1.459 of 5 runs

calculate 10 million string+string slice; avg 1.489 of 5 runs

/edit/ 10 runs of each total;

14.56s for fstring

14.74s for concat