Hi,
This is an interesting benchmark:
Given this function:
// ImagPowN computes iⁿ = (√-1)ⁿ
//
// i¹ = i i² = -1 i³ = -i i⁴ = 1
// i⁵ = i i⁶ = -1 i⁷ = -i i⁸ = 1
// i⁹ = i i¹⁰ = -1 i¹¹ = -i i¹² = 1
//
func ImagPowN(n int) complex128 {
if n == 0 {
return 1
}
switch n % 4 {
case 1:
return 1i
case 2:
return -1
case 3:
return -1i
}
return 1
}
And this benchmark test:
var (
imagpownRes complex128
)
func BenchmarkImagPowN(b *testing.B) {
var res complex128
for i := 0; i < b.N; i++ {
for n := 0; n < 200; n++ {
res = ImagPowN(n)
}
}
imagpownRes = res
}
func BenchmarkImagPowNcmplx(b *testing.B) {
var res complex128
for i := 0; i < b.N; i++ {
for n := 0; n < 200; n++ {
res = cmplx.Pow(1i, complex(float64(n), 0))
}
}
imagpownRes = res
}
We get this output ( go test -run=XXX -bench=. ):
BenchmarkImagPowN-32 3000000 470 ns/op
BenchmarkImagPowNcmplx-32 200000 10050 ns/op
A 20x speed up...
Cheers.
Dorival
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.