2020-04-29 17:29:00 +03:00
|
|
|
package timeprofile
|
2020-04-28 17:16:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
2020-04-29 17:29:00 +03:00
|
|
|
"time"
|
2020-04-28 17:16:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBigintProfile(t *testing.T) {
|
|
|
|
|
2020-04-29 17:29:00 +03:00
|
|
|
bigint := big.NewInt(14468)
|
2020-04-28 17:16:13 +03:00
|
|
|
|
2020-04-29 17:29:00 +03:00
|
|
|
BigIntFactorial(bigint)
|
2020-04-28 17:16:13 +03:00
|
|
|
|
|
|
|
bigint = big.NewInt(24566)
|
|
|
|
|
2020-04-29 17:29:00 +03:00
|
|
|
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)
|
|
|
|
}
|