Line ranges in Swift - Redux
Last week I published a post about finding the fastest way to retrieve line ranges from a large string. The way to determine which method was the fastest was very crude and I had this nagging feeling that it was also wrong. To save you the time of reading the last post again here is a quick summary. I processed a file of about 24k lines of assembly code that contained about 15 characters per line on average. I wrote about 6 different ways of finding the half-open ranges of each line, measured their runtime, and came to the following results:
- Using
String.Index
with a subscript: 24 milliseconds - Using a
Character
iterator and calculating the next index: 19 milliseconds - Zipping the
Character
andIndices
iterator together: 15 milliseconds - Using
enumerateLines
: 18 milliseconds - Using
lineRange
: 14 milliseconds - Using
split
: 14 milliseconds
These results meant that using lineRange
or split
were the fastest options but I can now say that those results were indeed wrong. The reasons for this were the very crude nature of my tests and some glaring errors regarding Xcode build optimisations which can be boiled down to my inexperience with Xcode. I ran all the tests again with a benchmarking framework and build the project from the command line with optimisations enabled. And this time I got the following results: