title: Python string join performance
body: <p>There are a lot of articles around the web concerning Python performance. The first thing you read is concatenating strings should not be done using '+'; avoid s1 + s2 + s3, and instead use <em>str.join</em></p>
  <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p>
  <ol>
  <li>'+' which I should not do</li>
  <li><em>str.join</em></li>
  <li><em>os.path.join</em></li>
  </ol>
  <p>Here is my code:</p>
  <pre><code>import os, time
  
  s1 = '/part/one/of/dir'
  s2 = 'part/two/of/dir'
  N = 10000
  
  t = time.clock()
  for i in xrange(N):
      s = s1 + os.sep + s2
  print time.clock() - t
  
  t = time.clock()
  for i in xrange(N):
      s = os.sep.join((s1, s2))
  print time.clock() - t
  
  t = time.clock()
  for i in xrange(N):
      s = os.path.join(s1, s2)
  print time.clock() - t
  </code></pre>
  <p>Here the results (Python 2.5 on <a href="https://en.wikipedia.org/wiki/Windows_XP" rel="nofollow noreferrer">Windows XP</a>):</p>
  <pre class="lang-none prettyprint-override"><code>0.0182201927899
  0.0262544541275
  0.120238186697
  </code></pre>
  <p>Shouldn't it be exactly the other way around?</p>