Optimize splitting vector of strings

This requires the substrings to be copied into Strings when they are stored in mat. If you instead use Matrix{SubString{String}}, then it won’t have to copy the strings at all.

Might also be easier to just put @views in front of the function, or in front of the for loop, and then you can just do:

mat[i, 1] = str[1:2]
mat[i, 2] = str[ifelse(space_idx==3, 1:0, 3:3)]
mat[i, 3] = str[space_idx+1:end]

instead of the explicit SubString calls. Notice that I also used the empty range str[1:0] instead of "" so that everything is a substring.

These two changes speed things up by almost a factor of 2 for me, as well as cutting the number of allocations down to 1.

8 Likes