Onkar Lapate’s Post

Why .join() is faster than str1 + str2 in Python? Why string concatenation gets really slow in Python? Strings are immutable in Python. When we write str1 + str2, following steps are done - 1. Request a brand new block of memory large enough for both. 2. Copy every single character from str1 and str2 into the new block. 3. Destroy the old strings (eventually) If we do this 10,000 times in a loop, we are copying millions of characters over and over again. When we use ''.join(list_of_strings), Python is significantly smarter. It performs following 2 pass operation - 1. It iterates through the entire list once to calculate the total length of the final string. 2. it allocates one single block of memory of that exact size and copies all the strings into their respective slots at once. Instead of n reallocations and n copies, one allocation and one copy! Takeaway - -> Need to join 2 or 3 strings - a + b is fine and very readable. -> Need to join strings in a loop - Always collect them in a list first and use ''.join(my_list) at the very end. I’m deep-diving into Python internals and performance. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories