Last active
September 16, 2017 06:28
-
-
Save lorneli/5e9be3e03d01cbcc984dfcf243c59cdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package node | |
const elemSize = 128 | |
type node struct { | |
inodes []*inode | |
} | |
type inode struct { | |
k []byte | |
v []byte | |
} | |
func createNode(size int) *node { | |
n := &node{} | |
n.inodes = make([]*inode, 0, size) | |
for i := 0; i < size; i++ { | |
in := &inode{k: []byte("boo"), v: []byte("bar")} | |
n.inodes = append(n.inodes, in) | |
} | |
return n | |
} | |
func (n *node) Size_AddElemSizeInLoop() int { | |
sz := 0 | |
for _, in := range n.inodes { | |
sz += elemSize + len(in.k) + len(in.v) | |
} | |
return sz | |
} | |
func (n *node) Size_SumElemSizeFirst() int { | |
sz := elemSize * len(n.inodes) | |
for _, in := range n.inodes { | |
sz += len(in.k) + len(in.v) | |
} | |
return sz | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package node | |
import ( | |
"testing" | |
) | |
func BenchmarkSize_AddElemSizeInLoop(b *testing.B) { | |
n := createNode(100) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
n.Size_AddElemSizeInLoop() | |
} | |
} | |
func BenchmarkSize_SumElemSizeFirst(b *testing.B) { | |
n := createNode(100) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
n.Size_SumElemSizeFirst() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[lorneli@localhost GoTest]$ go test -bench . | |
BenchmarkSize_AddElemSizeInLoop-4 10000000 197 ns/op | |
BenchmarkSize_SumElemSizeFirst-4 10000000 171 ns/op | |
PASS | |
ok _/mnt/hgfs/vmfolder/workspace/GoTest 4.176s | |
[lorneli@localhost GoTest]$ go test -bench . | |
BenchmarkSize_AddElemSizeInLoop-4 5000000 218 ns/op | |
BenchmarkSize_SumElemSizeFirst-4 10000000 173 ns/op | |
PASS | |
ok _/mnt/hgfs/vmfolder/workspace/GoTest 3.404s | |
[lorneli@localhost GoTest]$ go test -bench . | |
BenchmarkSize_AddElemSizeInLoop-4 10000000 188 ns/op | |
BenchmarkSize_SumElemSizeFirst-4 10000000 168 ns/op | |
PASS | |
ok _/mnt/hgfs/vmfolder/workspace/GoTest 4.071s | |
[lorneli@localhost GoTest]$ go test -bench . | |
BenchmarkSize_AddElemSizeInLoop-4 10000000 190 ns/op | |
BenchmarkSize_SumElemSizeFirst-4 10000000 169 ns/op | |
PASS | |
ok _/mnt/hgfs/vmfolder/workspace/GoTest 4.043s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment