A user of my app reported that it is slower when enumerating a large number of files on APFS compared to HFS+.
Online I can only find information that this is expected for rotational hard disks, but apparently APFS should be optimized for SSDs and hence the observed performance doesn't make sense.
I used the code below to enumerate a folder hierarchy with about 100'000 files. I ran it several times on both APFS and HFS+ to minimize differences due to caching. On APFS the last runs took about 4.5 seconds, while on HFS+ the last runs took about 1.3 seconds.
How can this big difference be explained? Is APFS really slower than HFS+ when enumerating files on SSDs?
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
if openPanel.runModal() == .cancel {
return
}
let resourceKeys: [URLResourceKey] = [.isDirectoryKey, .fileSizeKey, .creationDateKey, .contentModificationDateKey, .fileIdentifierKey, .isSymbolicLinkKey, .isHiddenKey, .isUserImmutableKey, .attributeModificationDateKey]
let enumerator = FileManager.default.enumerator(at: openPanel.urls[0], includingPropertiesForKeys: resourceKeys) { url, error in
print(error)
return true
}!
let date = Date()
var i = 0
for case let url as URL in enumerator {
let _ = try? url.resourceValues(forKeys: Set(resourceKeys))
i += 1
if i % 10000 == 0 {
print(i)
}
}
print(-date.timeIntervalSinceNow)