27 lines
543 B
Go
Raw Normal View History

2019-10-30 03:46:27 +08:00
package profile
import (
2020-05-06 17:23:37 +08:00
"log"
"math/big"
"time"
2019-10-30 03:46:27 +08:00
)
func Duration(invocation time.Time, name string) {
2020-05-06 17:23:37 +08:00
elapsed := time.Since(invocation)
2019-10-30 03:46:27 +08:00
2020-05-06 17:23:37 +08:00
log.Printf("%s lasted %s", name, elapsed)
2019-10-30 03:46:27 +08:00
}
2019-10-30 03:48:41 +08:00
func BigIntFactorial(x big.Int) *big.Int {
2020-05-06 17:23:37 +08:00
// Arguments to a defer statement is immediately evaluated and stored.
// The deferred function receives the pre-evaluated values when its invoked.
defer Duration(time.Now(), "IntFactorial")
2019-10-30 03:48:41 +08:00
2020-05-06 17:23:37 +08:00
y := big.NewInt(1)
2020-05-27 21:53:18 +08:00
for one := big.NewInt(1); x.Sign() > 0; x.Sub(&x, one) {
y.Mul(y, &x)
2020-05-06 17:23:37 +08:00
}
2019-10-30 03:48:41 +08:00
2020-05-06 17:23:37 +08:00
return x.Set(y)
2019-10-30 03:48:41 +08:00
}