Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 8d0e311

Browse files
committed
0069 原来,还是直接使用 math 更快
1 parent e944534 commit 8d0e311

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Algorithms/0069.sqrtx/sqrtx.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package problem0069
22

33
func mySqrt(x int) int {
44
res := x
5-
65
// 牛顿法求平方根
76
for res*res > x {
87
res = (res + x/res) / 2
98
}
10-
119
return res
1210
}

Algorithms/0069.sqrtx/sqrtx_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package problem0069
22

33
import (
44
"fmt"
5+
"math"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -64,3 +65,17 @@ func Test_Problem0069(t *testing.T) {
6465
ast.Equal(a.one, mySqrt(p.x), "输入:%v", p)
6566
}
6667
}
68+
69+
func Benchmark_mySqrt(b *testing.B) {
70+
n := 10000
71+
for i := 1; i < b.N; i++ {
72+
_ = mySqrt(n)
73+
}
74+
}
75+
76+
func Benchmark_math_sqrt(b *testing.B) {
77+
n := 10000
78+
for i := 1; i < b.N; i++ {
79+
_ = int(math.Sqrt(float64(n)))
80+
}
81+
}

0 commit comments

Comments
 (0)