-
-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathpointerperformace.go
More file actions
57 lines (40 loc) · 798 Bytes
/
Copy pathpointerperformace.go
File metadata and controls
57 lines (40 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"log"
"time"
)
func plusP(in *int) {
*in = *in + 1
}
func plusPpp(in *int) {
*in++
}
func plus(in int) int {
return in + 1
}
func main() {
var value int
var start time.Time
var elapsed time.Duration
value = 5
start = time.Now()
for i := 0; i < 1600000; i++ {
plusP(&value)
}
elapsed = time.Since(start)
log.Printf("calculated %v with pointer took: %s", value, elapsed)
value = 5
start = time.Now()
for i := 0; i < 1600000; i++ {
plusPpp(&value)
}
elapsed = time.Since(start)
log.Printf("calculated %v with pointer (++) took: %s", value, elapsed)
value = 5
start = time.Now()
for i := 0; i < 1600000; i++ {
value = plus(value)
}
elapsed = time.Since(start)
log.Printf("calculated %v without pointer took: %s", value, elapsed)
}