File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -98,7 +98,7 @@ A curated collection of idiomatic design & application patterns for Go language.
98
98
99
99
| Pattern | Description | Status |
100
100
| :-------:| :----------- | :------:|
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 | ✔ |
102
102
103
103
## Idioms
104
104
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments