Why are textwrap.wrap() and textwrap.fill() so slow?
<p>Why are <a href="http://docs.python.org/library/textwrap.html#textwrap.wrap" rel="nofollow"><code>textwrap.wrap()</code></a> and <a href="http://docs.python.org/library/textwrap.html#textwrap.fill" rel="nofollow"><code>textwrap.fill()</code></a> so slow? For example, to wrap a string of 10000 characters on my laptop takes nearly two and a half seconds.</p>

<pre><code>$ python -m timeit -n 10 -s 's = "A" * 10000; import textwrap' 'textwrap.fill(s)'
10 loops, best of 3: 2.41 sec per loop
</code></pre>

<p>Compare that to this code adapted from <a href="http://stackoverflow.com/a/2657733/38140">an answer to a related Stack Overflow question</a></p>

<pre><code>#!/usr/bin/env python
# simplewrap.py
def fill(text, width=70):
    return '\n'.join(text[i:i+width] for i in
                     range(0, len(text), width))
</code></pre>

<p>which wraps the text orders of magnitude faster than <code>textwrap</code>:</p>

<pre><code>$ python -m timeit -n 10 -s 's = "A" * 10000; import simplewrap' 'simplewrap.fill(s)'
10 loops, best of 3: 37.2 usec per loop
</code></pre>
