title: Replacing whole string is faster than replacing only its first character
body: <p>I tried to replace a character <code>a</code> by <code>b</code> in a given large string. I did an experiment - first I replaced it in the whole string, then I replaced it only at its beginning.</p>
  <pre class="lang-py prettyprint-override"><code>import re
  # pattern = re.compile('a')
  pattern = re.compile('^a')
  string = 'x' * 100000
  
  pattern.sub('b', string)
  </code></pre>
  <p>I expected that replacing the beginning would have to be much faster then replacing the whole string because you have to check only 1 position instead of 100000. I did some measuring:</p>
  <pre><code>python -m timeit --setup &quot;import re; p=re.compile('a'); string='x'*100000&quot; &quot;p.sub('b', string)&quot;
  10000 loops, best of 3: 19.1 usec per loop
  </code></pre>
  <pre><code>python -m timeit --setup &quot;import re; p=re.compile('^a'); string='x'*100000&quot; &quot;p.sub('b', string)&quot;
  1000 loops, best of 3: 613 usec per loop
  </code></pre>
  <p>The results show that, on the contrary, trying to replace the whole string is about 30x faster. Would you expect such result? Can you explain that?</p>