Splitting on white-space can be done in two ways with
str.strip. You can either issue a call with no arguments,
str.strip(), which defaults to using a white-space delimiter or explicitly supply the argument yourself with
But, why is it that when timed these functions perform so differently?
Using a sample string with an intentional amount of white spaces:
s = " " * 100 + 'a' + " " * 100
The timings for
s.strip(' ') are respectively:
%timeit s.strip() The slowest run took 32.74 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 396 ns per loop %timeit s.strip(' ') 100000 loops, best of 3: 4.5 µs per loop
strip(' ') takes
4.5 ?s, a similar scenario is present with
lsplit under the same conditions. Also,
bytes objects seem do be affected too.
The timings were performed for
Python 3.5.2, on
Python 2.7.1 the difference is less drastic. The docs on
str.split don't indicate anything useful, so, why does this happen?