[swift] Swift에서 파일 이동 시 이동에 필요한 시간 측정 방법 추가

파일을 이동하는 작업의 수행 시간을 측정하는 것은 중요합니다. 이러한 측정은 성능을 분석하거나 사용자 경험을 개선하는 데 도움이 될 수 있습니다. 이번 블로그에서는 Swift에서 파일 이동 시 걸리는 시간을 측정하는 방법을 알아보겠습니다.

파일 이동 시간 측정하기

파일을 이동하기 전과 이후에 현재 시간을 측정하여, 이 두 시간의 차이를 계산하여 파일 이동에 소요된 시간을 알아낼 수 있습니다.

다음은 Swift에서 파일 이동 시간을 측정하는 예제 코드입니다.

import Foundation

func moveFile(from sourcePath: String, to destinationPath: String) {
    let fileManager = FileManager.default

    let startTime = Date()

    do {
        try fileManager.moveItem(atPath: sourcePath, toPath: destinationPath)
        let endTime = Date()
        let timeDifference = endTime.timeIntervalSince(startTime)
        print("File moved in \(timeDifference) seconds")
    } catch {
        print("Error: \(error)")
    }
}

let sourcePath = "path/to/source/file"
let destinationPath = "path/to/destination/file"

moveFile(from: sourcePath, to: destinationPath)

위의 예제 코드에서, moveFile 함수는 파일을 이동하는 데 걸린 시간을 측정합니다. moveItem 메서드를 사용하여 파일을 이동하고, 그 전후의 시간을 측정하여 걸린 시간을 출력합니다.

이제 파일 이동 시 걸리는 시간을 측정하기 위한 방법을 알게 되었습니다. 이를 통해 파일 이동 작업의 성능을 분석하고 개선할 수 있을 것입니다.

참고 자료

Swift FileManager Documentation