写了一个线性二分查找的算法,想打印出执行的时间,时间明明在7秒以上,可却打印出2.xxms 或者 7.xxms
,总之,打印出来的数字都不到一秒。奇怪,代码如下,望大神指点:
```golang
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}
func main() {
array := makeRange(0, 1000000000)
LinearySearcheT(array)
}
func LinearySearcheT(array []int) {
defer timeCost("线性查找 ", time.Now())
bool := LinearSearch(array, 1000000)
fmt.Print("b1:", bool)
}
func timeCost(desc string, start time.Time) {
terminal := time.Since(start)
fmt.Printf("%v耗时:%v\n", desc, terminal)
}
func LinearSearch(array []int, t int) bool {
i := 0
for i < len(array) {
if array[i] == t {
return true
}
i++
}
return false
}
```
有疑问加站长微信联系(非本文作者)