I have a lot of strings like this one:
"substring1:substring2:...:substring9"
So the number of substrings in string is always 9, and some substrings in string may be empty.
I want to split the string by ":" into array of strings and i do it like this:
let separator = Character(":")
let arrayOfStrings = string.split(separator: separator, maxSplits: 8, omittingEmptySubsequences: false).map({ String($0) })
For example for 13.5k strings it took about 150ms to convert them into arrays of strings.
Is there any other method that is more efficient in terms of time for this task?
let arrayOfStrings = string.components(separatedBy: ":")might be quicker, since you don't need amap()afterward (which is creating another loop).SubStringinstead, and avoid the conversion toString.splitwhich take a lot of time.mapis very powerful but not very time-savior (remember that this will make a new object on every iteration)