Disparity between Swift enumerator and GNU find

43 views Asked by At

I have written a GNU find-like emulator in Swift out of curiosity, and have stumbled upon this time disparity:

$ time find / &>/dev/null
real    1m6.040s

$ time swiftly-find /
real    5m43.028s

The Swift code is as follows:

let enumeration = FileManager.default.enumerator(at: URL(string: "\(from)/")!, includingPropertiesForKeys: nil, errorHandler: { (at: URL, with: Error) in
    print("Error \(with) encountered at directory \(at)"); return true
})!

var list = String()

for i in enumeration {
    let result = String(describing: i).replacingOccurrences(of: "file://", with: "")
    list.append("\(result)\n")
    print(result)
}

Why is it that Swift's enumerator is so significantly slower than GNU find? Is there a way to speed up the Swift code to reach similar speeds as find?

0

There are 0 answers