Skip to content

Commit

Permalink
Benchmark two ways of getting pointer to int.
Browse files Browse the repository at this point in the history
Both ways end up generating same code using gc.

	BenchmarkOldInt-8	100000000	        22.7 ns/op	       8 B/op	       1 allocs/op
	BenchmarkNewInt-8	100000000	        22.9 ns/op	       8 B/op	       1 allocs/op
  • Loading branch information
dmitshur committed May 9, 2016
1 parent d7c2320 commit 4883bde
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 190/main.go
@@ -0,0 +1,4 @@
// Benchmark two ways of getting pointer to int.
package main

func main() {}
31 changes: 31 additions & 0 deletions 190/main_test.go
@@ -0,0 +1,31 @@
package main

import "testing"

var result *int

func OldInt(v int) *int {
p := new(int)
*p = v
return p
}

func NewInt(v int) *int {
return &v
}

func BenchmarkOldInt(b *testing.B) {
var r *int
for i := 0; i < b.N; i++ {
r = OldInt(i)
}
result = r
}

func BenchmarkNewInt(b *testing.B) {
var r *int
for i := 0; i < b.N; i++ {
r = NewInt(i)
}
result = r
}

0 comments on commit 4883bde

Please sign in to comment.