Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't care about sub strings than a simple</p> <pre><code>&gt;&gt;&gt; 'a short sized string with spaces '.split() </code></pre> <p>Performance:</p> <pre><code>&gt;&gt;&gt; s = " ('a short sized string with spaces '*100).split() " &gt;&gt;&gt; t = timeit.Timer(stmt=s) &gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) 171.39 usec/pass </code></pre> <p>Or string module</p> <pre><code>&gt;&gt;&gt; from string import split as stringsplit; &gt;&gt;&gt; stringsplit('a short sized string with spaces '*100) </code></pre> <p>Performance: String module seems to perform better than string methods</p> <pre><code>&gt;&gt;&gt; s = "stringsplit('a short sized string with spaces '*100)" &gt;&gt;&gt; t = timeit.Timer(s, "from string import split as stringsplit") &gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) 154.88 usec/pass </code></pre> <p>Or you can use RE engine</p> <pre><code>&gt;&gt;&gt; from re import split as resplit &gt;&gt;&gt; regex = '\s+' &gt;&gt;&gt; medstring = 'a short sized string with spaces '*100 &gt;&gt;&gt; resplit(regex, medstring) </code></pre> <p>Performance</p> <pre><code>&gt;&gt;&gt; s = "resplit(regex, medstring)" &gt;&gt;&gt; t = timeit.Timer(s, "from re import split as resplit; regex='\s+'; medstring='a short sized string with spaces '*100") &gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000) 540.21 usec/pass </code></pre> <p>For very long strings you should not load the entire string into memory and instead either split the lines or use an iterative loop</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload