0

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?

perhapsmaybeharry
  • 874
  • 3
  • 13
  • 32
  • You should *profile* your code in Instruments. Did you compile with optimizations? – One difference is that your Swift code creates a huge array in memory, whereas the GNU utility most probably prints the results without caching them all. – Martin R Oct 12 '16 at 11:15
  • @MartinR I see. Would it be reasonable to expect `find`-like performance from Swift's `enumerator`? – perhapsmaybeharry Oct 12 '16 at 14:14
  • I don't know. `find` is probably pretty much optimized for its purpose, whereas FileManager is a universal tool. So I wouldn't be surprised if it is slower. Profile it in Instruments and try to find where the bottlenecks are. – Martin R Oct 12 '16 at 14:19

0 Answers0