<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Geordy Van Cutsem on Medium]]></title>
        <description><![CDATA[Stories by Geordy Van Cutsem on Medium]]></description>
        <link>https://medium.com/@juwot?source=rss-f9986198d655------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*NJSqABa1FqBTg_nP.</url>
            <title>Stories by Geordy Van Cutsem on Medium</title>
            <link>https://medium.com/@juwot?source=rss-f9986198d655------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 05:26:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@juwot/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Python: I was wrong about the speed of concatenation]]></title>
            <link>https://medium.com/@juwot/python-i-was-wrong-about-the-speed-of-concatenation-83b5e0851b74?source=rss-f9986198d655------2</link>
            <guid isPermaLink="false">https://medium.com/p/83b5e0851b74</guid>
            <category><![CDATA[research]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[speed]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[test]]></category>
            <dc:creator><![CDATA[Geordy Van Cutsem]]></dc:creator>
            <pubDate>Sat, 23 May 2020 16:05:13 GMT</pubDate>
            <atom:updated>2020-05-23T17:23:35.680Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gh5N8Qkt9d9mYa2P1N8L8A.jpeg" /></figure><p>Github repository: <a href="https://github.com/geordyvcErasmus/string_concatenation">https://github.com/geordyvcErasmus/string_concatenation</a><br>Tested on Python 3.7.4</p><p>A few months ago I came across some web pages that explained the fastest way for concatenating strings in Python. Now why was I looking for such a trivial thing? I was working with huge datasets and importing that data needed to be under a certain time limit. Now shaving of a few nanoseconds for each import, would make a difference of maybe 15 to 30 minutes in the end. And since the code base was not too large, this was an easy win. I happened to come across the .format() function, so that’s what i implemented. But last week this issue popped up in my head again after not touching Python for while, so now I wanted to verify what I implemented was legit the fastest way. And boy did I have it wrong.</p><p>So I made script with all the different methods that I could find. I put the link to the script on Github at the top of this article if you want to have a look. Also if you have an issue with how I tested it, or something is missing. You’re welcome to make an issue or just let me know here in a comment. Always open for learning more.</p><pre>import time<br><br><br>if __name__ == &quot;__main__&quot;:<br>    # declare our two strings.<br>    the_first_string = &quot;hello&quot;<br>    the_second_string = &quot;world&quot;<br>    LOOP_COUNT = 300000<br><br>    first_test_start_time = time.time()<br>    for i in range(LOOP_COUNT):<br>        # The easy way<br>        combined_string = the_first_string + &quot; &quot; + the_second_string<br>    first_test_end_time = time.time()<br>    print(&quot;first test&quot;)<br>    print(first_test_end_time-first_test_start_time)<br><br>    second_test_start_time = time.time()<br>    for i in range(LOOP_COUNT):<br>        # with the % operator<br>        combined_string = &quot;%s %s&quot; % (the_first_string, the_second_string)<br>        # I personally like this way. It is easy to use and for me I immediately know what is happening.<br>    second_test_end_time = time.time()<br>    print(&quot;second test&quot;)<br>    print(second_test_end_time-second_test_start_time)<br><br>    third_test_start_time = time.time()<br>    for i in range(LOOP_COUNT):<br>        # with .format()<br>        combined_string = &quot;{} {}&quot;.format(the_first_string, the_second_string)<br>    third_test_end_time = time.time()<br>    print(&quot;third test&quot;)<br>    print(third_test_end_time-third_test_start_time)<br><br>    fourth_test_start_time = time.time()<br>    for i in range(LOOP_COUNT):<br>        # with the .join method<br>        combined_string = &quot;&quot;.join([the_first_string, &#39; &#39;, the_second_string])<br>        # I find this the least readable format. I do not recommend this.<br>        # Unless dumping a list to string, this could be useful.<br>    fourth_test_end_time = time.time()<br>    print(&quot;fourth test&quot;)<br>    print(fourth_test_end_time-fourth_test_start_time)<br><br>    fifth_test_start_time = time.time()<br>    for i in range(LOOP_COUNT):<br>        # with f&quot;{variable}&quot;<br>        combined_string = f&#39;{the_first_string} {the_second_string}&#39;<br>        # I actually came across this while making this script and I also like this.<br>    fifth_test_end_time = time.time()<br>    print(&quot;fifth test&quot;)<br>    print(fifth_test_end_time-fifth_test_start_time)</pre><p>So after running this script the results where as following:</p><pre>first test<br>0.039998531341552734<br>second test<br>0.0769963264465332<br>third test<br>0.07900238037109375<br>fourth test<br>0.047000885009765625<br>fifth test<br>0.033998727798461914</pre><p>The last way of concatenating with f”{variable_name}” is the fastest way. Now I only took into account concatenating string variables and not other types that first need to be converted to strings because this was irrelevant to the test in my opinion.</p><p><strong>Conclusion</strong></p><p>This was the fastest way. I hope you got some value from this short article and happy coding.</p><pre># with f&quot;{variable}&quot;<br>combined_string = f&#39;{the_first_string} {the_second_string}&#39;<br># I actually came across this while making this script and I also like this.</pre><p>Picture taken from <a href="https://pexels.com/">https://pexels.com</a> by Christina Morillo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83b5e0851b74" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>