JuliaLang/julia — pull request #5451 by tanmaykm (2014-01-20 08:24:24+00:00)
Title: Faster bytestring for SubString{ByteString} and splitsub to split into substrings

This PR adds:
- a specialized faster `bytestring` method for substrings
- splitsub & rsplitsub methods that return substrings instead of strings

This can probably address #3667 and #1250.
Code duplication could have been avoided by calling `splitsub` from `split` and converting the result into strings, but that would be less optimal.

Below are some time comparisons for these changes.

Time comparisons for `bytestring` (source below):
````
# with specialized bytestring for SubStrings:
elapsed time: 2.591398653 seconds (880001560 bytes allocated)
# without
elapsed time: 8.966283881 seconds (2640001560 bytes allocated)
````

Time comparisons for `splitsub` (source below):
````
splitsub:
elapsed time: 2.363995988 seconds (588436464 bytes allocated)
split:
elapsed time: 4.266700792 seconds (1068436464 bytes allocated)
````
A `splitsub` + `convert` results in:
````
elapsed time: 5.18554128 seconds (1468438024 bytes allocated)
````

Source for `bytestring` time comparison:
````
const nstr = 10^7
const lstr = 6

function makestr()
iob = IOBuffer()
for idx in 1:nstr
write(iob, randstring(lstr))
write(iob, ' ')
end
takebuf_string(iob)
end

function timeconvert()
s = makestr()
substrs = matchall(r"[^\s]+", s)
gc()
@time convert(Array{ASCIIString, 1}, substrs)
end

timeconvert()
````

Source for `splitsub` time comparison:
````
const nstr = 10^7
const lstr = 6

function makestr()
iob = IOBuffer()
for idx in 1:nstr
write(iob, randstring(lstr))
write(iob, ' ')
end
takebuf_string(iob)
end

function fss(str::String)
gc()
@time astr = splitsub(str)
end

function fs(str::String)
gc()
@time split(str)
end

function f()
str = makestr()
println("splitsub:")
fss(str)
println("split:")
fs(str)
end

f()
````