Calculate the execution time of a sort algorithm with Swift

We have to calculate the execution time for 2 different algorithms then determine recommend one based on execution time.
We are new to this and not able to manage this.

func printTimeElapsedWhenRunningCode(title:String, operation:()->()) {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed for (title): (timeElapsed) s.")
}
The trying to test:
printTimeElapsedWhenRunningCode(title: "Merge Sort") {
let newSort = mergeSort(sortArray)
}
Every time we run this we get a different time. We do not know it it right or wrong.
Please help.
Thanks in advance.

Sign In or Register to comment.