Skip to content

Commit 28484de

Browse files
committed
add square root performance test
1 parent d90370b commit 28484de

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package squareroot
2+
3+
import (
4+
"math"
5+
)
6+
7+
func squareRoot(n int) float64 {
8+
var fa float64
9+
for i := 0; i < n; i++ {
10+
fa = math.Sqrt(10.0)
11+
}
12+
13+
return fa
14+
}
15+
16+
func squareRootVariable(n int) float64 {
17+
var fa float64
18+
for i := 0; i < n; i++ {
19+
fa = math.Sqrt(float64(i))
20+
}
21+
22+
return fa
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// +build all column7 squareroot square root perf
2+
3+
package squareroot
4+
5+
import (
6+
"testing"
7+
8+
"github.com/LuigiAndrea/GoPearls/utilities"
9+
goth "github.com/LuigiAndrea/test-helper/messages"
10+
)
11+
12+
var steps int = 1000000000
13+
14+
func TestCallSquareRoot(t *testing.T) {
15+
defer utilities.Elapse(goth.GetFuncName(squareRoot))()
16+
squareRoot(steps)
17+
}
18+
19+
func TestCallSquareRootVariable(t *testing.T) {
20+
defer utilities.Elapse(goth.GetFuncName(squareRootVariable))()
21+
squareRootVariable(steps)
22+
}

Diff for: utilities/helper.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package utilities
22

33
import (
4+
"fmt"
45
"sort"
6+
"time"
57
)
68

79
//Reverse a slice
@@ -26,3 +28,12 @@ func PreAppend(list []interface{}, elements ...interface{}) []interface{} {
2628
}
2729
return list
2830
}
31+
32+
//Elapse calculate the time elapsed
33+
func Elapse(name string) func() {
34+
start := time.Now()
35+
36+
return func() {
37+
fmt.Printf("%s took %v\n", name, time.Since(start))
38+
}
39+
}

0 commit comments

Comments
 (0)