Skip to content

Instantly share code, notes, and snippets.

@woodsaj
Created August 17, 2016 07:12
Show Gist options
  • Save woodsaj/40a0dec0a1b7f4109c580ef2ade0c829 to your computer and use it in GitHub Desktop.
Save woodsaj/40a0dec0a1b7f4109c580ef2ade0c829 to your computer and use it in GitHub Desktop.
ptr vs value benchmark
package main
import (
"testing"
"strconv"
)
type Data struct {
Name string
Tags []string
}
type MapWithPtrs struct {
Items map[int]*Data
}
type MapWithoutPtrs struct {
Items map[int]Data
}
var (
mwp *MapWithPtrs
mwop *MapWithoutPtrs
)
func init() {
mwp = &MapWithPtrs{
Items: make(map[int]*Data),
}
for i:=0; i < 5000000; i++ {
mwp.Items[i] = &Data{
Name: "data"+strconv.Itoa(i),
Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag"+strconv.Itoa(i)},
}
}
mwop = &MapWithoutPtrs{
Items: make(map[int]Data),
}
for i:=0; i < 5000000; i++ {
mwop.Items[i] = Data{
Name: "data"+strconv.Itoa(i),
Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag"+strconv.Itoa(i)},
}
}
}
func BenchmarkPtr(b *testing.B) {
for n:=0; n <b.N; n++ {
if data, ok := mwp.Items[n]; ok {
data.Tags = append(data.Tags, "blah")
}
}
}
func BenchmarkNoPtr(b *testing.B) {
for n:=0; n <b.N; n++ {
if data, ok := mwop.Items[n]; ok {
data.Tags = append(data.Tags, "blah")
mwop.Items[n] = data
}
}
}
@Dieterbe
Copy link

package main

import (
    "strconv"
    "testing"
)

var size = 5 * 1000 * 1000

type Data struct {
    Name string
    Tags []string
}

type MapWithPtrs struct {
    Items map[int]*Data
}

type MapWithoutPtrs struct {
    Items map[int]Data
}

var (
    mwp  *MapWithPtrs
    mwop *MapWithoutPtrs
    lwp  []*Data
    lwop []Data
)

func init() {
    mwp = &MapWithPtrs{
        Items: make(map[int]*Data),
    }
    for i := 0; i < size; i++ {
        mwp.Items[i] = &Data{
            Name: "data" + strconv.Itoa(i),
            Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag" + strconv.Itoa(i)},
        }
    }
    mwop = &MapWithoutPtrs{
        Items: make(map[int]Data),
    }
    for i := 0; i < size; i++ {
        mwop.Items[i] = Data{
            Name: "data" + strconv.Itoa(i),
            Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag" + strconv.Itoa(i)},
        }
    }

    lwp = make([]*Data, size)
    for i := 0; i < size; i++ {
        lwp[i] = &Data{
            Name: "data" + strconv.Itoa(i),
            Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag" + strconv.Itoa(i)},
        }
    }

    lwop = make([]Data, size)
    for i := 0; i < size; i++ {
        lwop[i] = Data{
            Name: "data" + strconv.Itoa(i),
            Tags: []string{"foo", "baz", "bar", "one", "two", "three", "tag" + strconv.Itoa(i)},
        }
    }
}

func BenchmarkMapPtr(b *testing.B) {
    times := 1
    if b.N >= size {
        times = b.N/size + 1
        b.N = size
    }
    for i := 1; i <= times; i++ {
        for n := 0; n < b.N; n++ {
            data := mwp.Items[n]
            data.Tags = append(data.Tags, "blah")
        }
    }
}

func BenchmarkMapNoPtr(b *testing.B) {
    times := 1
    if b.N >= size {
        times = b.N/size + 1
        b.N = size
    }
    for i := 1; i <= times; i++ {
        for n := 0; n < b.N; n++ {
            data := mwop.Items[n]
            data.Tags = append(data.Tags, "blah")
            mwop.Items[n] = data
        }
    }
}

func BenchmarkListPtr(b *testing.B) {
    times := 1
    if b.N >= size {
        times = b.N/size + 1
        b.N = size
    }
    for i := 1; i <= times; i++ {
        for n := 0; n < b.N; n++ {
            data := lwp[n]
            data.Tags = append(data.Tags, "blah")
        }
    }
}

func BenchmarkListNoPtr(b *testing.B) {
    times := 1
    if b.N >= size {
        times = b.N/size + 1
        b.N = size
    }
    for i := 1; i <= times; i++ {
        for n := 0; n < b.N; n++ {
            lwop[n].Tags = append(lwop[n].Tags, "blah")
        }
    }
}
~/test2 ❯❯❯ go test -bench=.
testing: warning: no tests to run
BenchmarkMapPtr-8        5000000           326 ns/op
BenchmarkMapNoPtr-8      5000000           491 ns/op
BenchmarkListPtr-8       5000000           323 ns/op
BenchmarkListNoPtr-8     5000000           308 ns/op
PASS
ok      _/home/dieter/test2 110.952s
~/test2 ❯❯❯ go test -bench=.
testing: warning: no tests to run
BenchmarkMapPtr-8        5000000           324 ns/op
BenchmarkMapNoPtr-8      5000000           503 ns/op
BenchmarkListPtr-8       5000000           297 ns/op
BenchmarkListNoPtr-8     5000000           330 ns/op
PASS
ok      _/home/dieter/test2 110.012s

@Dieterbe
Copy link

same outcome when I change BenchmarkListPtr to use

    for i := 1; i <= times; i++ {
        for n := 0; n < b.N; n++ {
            lwp[n].Tags = append(lwp[n].Tags, "blah")
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment