2016-09-14 15:49:43 +03:00
|
|
|
# Timing Functions
|
|
|
|
|
|
|
|
When optimizing code, sometimes a quick and dirty time measurement is required
|
|
|
|
as opposed to utilizing profiler tools/frameworks to validate assumptions.
|
|
|
|
|
|
|
|
Time measurements can be performed by utilizing `time` package and `defer` statements.
|
|
|
|
|
|
|
|
## Implementation
|
|
|
|
|
|
|
|
```go
|
|
|
|
package profile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Duration(invocation time.Time, name string) {
|
|
|
|
elapsed := time.Since(invocation)
|
|
|
|
|
|
|
|
log.Printf("%s lasted %s", name, elapsed)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```go
|
2017-02-22 19:05:18 +03:00
|
|
|
func BigIntFactorial(x big.Int) *big.Int {
|
2016-09-14 15:49:43 +03:00
|
|
|
defer profile.Duration(time.Now(), "IntFactorial")
|
|
|
|
|
|
|
|
y := big.NewInt(1)
|
|
|
|
for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) {
|
|
|
|
y.Mul(y, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
return x.Set(y)
|
|
|
|
}
|
|
|
|
```
|