When copying a 7 GB file, the filecopy
function is always about 20% slower than the cp
command. I tested using my MacBook Pro 14" M1 and an external SSD connected via USB.
-
Mac to Mac
- filecopy: 4.78s
- cp: 3.40s
-
Mac to SSD
- filecopy: 20.22s
- cp: 15.93s
-
SSD to Mac
- filecopy: 20.61s
- cp: 16.64s
I use the following code:
let openPanel = NSOpenPanel()
openPanel.runModal()
let source = openPanel.urls[0]
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.runModal()
let destination = openPanel.urls[0].appendingPathComponent(source.lastPathComponent)
source.withUnsafeFileSystemRepresentation { sourcePath in
destination.withUnsafeFileSystemRepresentation { destinationPath in
let state = copyfile_state_alloc()
defer {
copyfile_state_free(state)
}
let date = Date()
if copyfile(sourcePath, destinationPath, state, copyfile_flags_t(COPYFILE_ALL | COPYFILE_NOFOLLOW | COPYFILE_EXCL)) != 0 {
print(NSError(domain: NSPOSIXErrorDomain, code: Int(errno)))
}
print(-date.timeIntervalSinceNow)
}
}
and the very basic cp
command:
time cp /path/to/source /path/to/destination
Is there a faster way to copy files? Am I doing something wrong with filecopy
?