go-pattern-examples/gomore/28_profiles/time_profile_test.go

43 lines
653 B
Go
Raw Normal View History

package timeprofile
2020-04-28 17:16:13 +03:00
import (
"log"
"math/big"
"testing"
"time"
2020-04-28 17:16:13 +03:00
)
func TestBigintProfile(t *testing.T) {
bigint := big.NewInt(14468)
2020-04-28 17:16:13 +03:00
BigIntFactorial(bigint)
2020-04-28 17:16:13 +03:00
bigint = big.NewInt(24566)
BigIntFactorial(bigint)
2020-04-28 17:16:13 +03:00
}
//Duration for time differ
func Duration(invocation time.Time, name string) {
elapsed := time.Since(invocation)
log.Printf("%s lasted %s", name, elapsed)
}
//BigIntFactorial ...
func BigIntFactorial(input *big.Int) *big.Int {
//关键点是这一句.
defer Duration(time.Now(), "IntFactorial")
x := input
y := big.NewInt(1)
for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) {
y.Mul(y, x)
}
return x.Set(y)
}