Skip to content

Commit 90f5b5b

Browse files
committed
profiling/timing: implement execution time profiler
1 parent 418cd23 commit 90f5b5b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ A curated collection of idiomatic design & application patterns for Go language.
9898

9999
| Pattern | Description | Status |
100100
|:-------:|:----------- |:------:|
101-
| [Timing Functions](/profiling/timing.md) | Wraps a function and logs the execution | |
101+
| [Timing Functions](/profiling/timing.md) | Wraps a function and logs the execution | |
102102

103103
## Idioms
104104

profiling/timing.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Timing Functions
2+
3+
When optimizing code, sometimes a quick and dirty time measurement is required
4+
as opposed to utilizing profiler tools/frameworks to validate assumptions.
5+
6+
Time measurements can be performed by utilizing `time` package and `defer` statements.
7+
8+
## Implementation
9+
10+
```go
11+
package profile
12+
13+
import (
14+
"time"
15+
"log"
16+
)
17+
18+
func Duration(invocation time.Time, name string) {
19+
elapsed := time.Since(invocation)
20+
21+
log.Printf("%s lasted %s", name, elapsed)
22+
}
23+
```
24+
25+
## Usage
26+
27+
```go
28+
func IntFactorial(x big.Int) *big.Int {
29+
defer profile.Duration(time.Now(), "IntFactorial")
30+
31+
y := big.NewInt(1)
32+
for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) {
33+
y.Mul(y, x)
34+
}
35+
36+
return x.Set(y)
37+
}
38+
```

0 commit comments

Comments
 (0)