Count the number of lines in a Swift String

后端 未结 3 1189
天涯浪人
天涯浪人 2021-01-14 10:15

After reading a medium sized file (about 500kByte) from a web-service I have a regular Swift String (lines) originally encoded in .isolatin1. Befor

3条回答
  •  情歌与酒
    2021-01-14 10:39

    As I did not find a generic way to count newlines I ended up just solving my problem by iterating through all the characters using

    let linesCount = text.reduce(into: 0) { (count, letter) in
         if letter == "\r\n" {      // This treats CRLF as one "letter", contrary to UnicodeScalars
            count += 1
         }
    }
    

    I was sure this would be a lot faster than enumerating lines for just counting, but I resolved to eventually do the measurement. Today I finally got to it and found ... that I could not have been more wrong.

    A 10000 line string counted lines as above in about 1.0 seconds , but counting through enumeration using

    var enumCount = 0
    text.enumerateLines { (str, _) in
        enumCount += 1
    }
    

    only took around 0.8 seconds and was consistently faster by a little more than 20%. I do not know what tricks the Swift engineers hide in their sleves, but they sure manage to enumerateLines very quickly. This just for the record.

提交回复
热议问题