简体   繁体   中英

Performance degradation on reading .txt files in Swift

I have a text file filled with numbers and reading the content of file as follows

func arrayFromContentsOfFileWithName(fileName: String) -> [String]? {
guard let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "txt") else {
    return nil
}

do {
    let content = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
    return content.componentsSeparatedByString("\n")
} catch _ as NSError {
    return nil
}}

However, when I check for performance, there seems to be a bottleneck at content.componentsSeparatedByString("\\n") as shown below:

性能下降示例

Can someone suggest a way of dealing with this problem? or may be read the entire text file as double in one-go with values separated from each other as follows:

let B1: [Float] = [0.0584000014, 0.0905999988, 0.0916000009, 0.0640999973, 0.0824000016, 0.0960000008, 0.0776999965, 0.0936999992, 0.0908999965, 0.0568999983, 0.0654999986, 0.0535999984, 0.0901999995, 0.0724999979, 0.104900002, 0.0798000023, 0.0962999985, 0.0914999992, 0.0680999979, 0.110100001, 0.0648000017, 0.103299998, 0.077200003, 0.0821999982, 0.0778999999, 0.074000001, 0.0710999966, 0.108499996, 0.060899999, 0.0697000027, 0.0841000006, 0.061900001]

That is because creating new String is slow. I haven't looked into Swift's source code to understand why, but that seems to be a trait Swift carried over from ObjC. On top of that, you have to convert the String into Float , which is not a fast operation either.

Use NSScanner instead:

func timeBlock(label: String, block: () -> Void) {
    let start = NSDate().timeIntervalSince1970
    block()
    let end = NSDate().timeIntervalSince1970

    print("\(label): \(end - start) seconds")
}

func f1 (fileContent: String) -> [Float] {
    return fileContent.componentsSeparatedByString("\n")
                .flatMap { Float($0) }
}

func f2 (fileContent: String) -> [Float] {
    let scanner = NSScanner(string: fileContent)
    var result = [Float]()

    while !scanner.atEnd {
        var x: Float = 0
        scanner.scanFloat(&x)
        result.append(x)
    }

    return result
}


do {
    let content = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
    timeBlock("f1") { f1(content) }
    timeBlock("f2") { f2(content) }
} catch let error as NSError {
    print(error.localizedDescription)
}

I created a random file with 1M lines of numbers as input. NSScanner is about 5.3x faster:

f1: 4.2731032371521 seconds
f2: 0.82185697555542 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM