基准测试
测试不仅要测试逻辑,还有一个很重要的一点是性能。
我对其他语言了解不深,我不知道其他语言在测试性能时是怎么做的。但我感觉,go test在测试性能上绝对是数一数二的便利。
写一个基准测试
sort.go
package sort
/**
* 从小到大排序
*/
//冒泡排序
func BubbleSort(a []int) []int{
lenth := len(a)
for i := 0; i < lenth; i++{
for j := i+1; j < lenth; j++ {
if a[j]
a[j], a[i] = a[i], a[j]
}
}
}
return a
}
//选择排序
func SelectSort(a []int) []int {
lenth := len(a)
var minIndex int
for i := 0; i < lenth; i++ {
minIndex = i;
for j := i+1; j < lenth; j++ {
if a[j] < a[minIndex] {
minIndex = j;
}
}
a[i], a[minIndex] = a[minIndex], a[i]
}
return a
}
//插入排序
func InsertSort(a []int) []int {
lenth := len(a)
for i := 1; i < lenth; i++{
index := i-1
number := a[i]
for index >= 0 && number