When we need to test the performance of the program, the test function that comes with Xcode sometimes cannot meet our needs. At this time, we need to write the test code ourselves. Performance testing is mainly to test the running time of programs or codes. Whether it is testing CPU/GPU-intensive programs or read-write-intensive programs, it is necessary to obtain the running time of programs or functions to calculate performance.
If you are testing a CLI (Command Line Interface, command line interaction) program, you can add commands when using the program time
to get the running time of the entire program. But if you want to test the running time of a certain function in the program code, or when testing the GUI program, this method will not work. We need to add a "timer" to the code to arrange the measurement work by ourselves.
There are two ways to implement this measurement in Swift, the first one is more convenient but can only be used on new systems, the second one is more compatible but not safe (slightly less accurate, but negligible).
Easy way on new systemsmeasure
However, starting with iOS 16 and macOS 13, Apple has added new data structures Clock
and ContinuousClock
methods measure
, which can be used to test the running time of a function.
It is also very simple to use.
First, instance one ContinuousClock
:
let clock = ContinuousClock()
Then use the following statement to get the running time of a function:
let elapsed = clock.measure {
someWork()
}
It should be noted here that let
it is mandatory to use, and you cannot declare a variable before and then instantiate it here.
The type obtained here elapsed
needs ContinuousClock.Instant.Duration
to be converted into a string for use. The conversion method is as follows:
var duration: String = ""
let elapsed = clock.measure {
quicksort()
}
//elapsed.description是字符串类型
duration = elapsed.description
The overall code is:
struct ContentView: View {
var clock = ContinuousClock()
@State private var duration: String = ""
var body: some View {
VStack {
Text("点击按钮进行快速排序测试:")
.padding()
Button(action: {
//获取时间间隔,也就是运行时间
let elapsed = clock.measure {
//测试函数为‘quicksort’
quicksort()
}
//把获取到的时间间隔转换成字符串
duration = elapsed.description
}) {
Text("Start")
}
.padding()
// 显示时间,由于elapsed.description最后自带字符串“second”,所以不用加单位
Text("消耗时间为:\(duration)")
.padding()
}
.padding()
}
}
Here's what it looks like running in the App:
Common method
If it is not the latest system, or if you want to write related method functions yourself, then you can use the classic method: time difference. Record the start time and end time, and then make the difference to get the running time.
So what is this time based on? The C language is clock time, but Swift has only clock time since iOS 16, and only Date
types before. Fortunately, Date can achieve our purpose, timeIntervalSince1970
just use the value in it.
timeIntervalSince1970
This value is a very historical value, and all systems (not just Apple's) have this value, because 1970 is the start time of the first widely used system Unix, and the records are from timeIntervalSince1970
1970- Starting from 01-01 00:00:00, the precision can reach the decimal point and reach milliseconds (or even smaller, similar to the precision of the previous method) for the elapsed time at this moment.
Since there is only a part of the difference from the previous method, the overall code is listed directly:
struct ContentView: View {
@State private var duration: String = ""
var body: some View {
VStack {
Text("点击按钮进行快速排序测试:")
.padding()
Button(action: {
//记录开始时间
let start_time = Date().timeIntervalSince1970
//开始运行被测试函数
quicksort()
//记录结束时间
let end_time = Date().timeIntervalSince1970
//做差获取间隔时间,并且将其变成字符串格式
duration = TimeInterval(end_time-start_time).description
}) {
Text("Start")
}
.padding()
//由于这个方法没有带单位,所以自己加一个“seconds”
Text("消耗时间为:\(duration) seconds")
.padding()
}
.padding()
}
}
The operation effect is exactly the same as the previous method, so it is not listed.
Hope to help those in need~