Created
January 26, 2018 14:33
-
-
Save barryz/0cf9b00b7cee3746b8b860521c87af18 to your computer and use it in GitHub Desktop.
Benchmark interface map and origin type map
This file contains hidden or 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 main | |
import ( | |
"fmt" | |
"testing" | |
) | |
type Tester interface { | |
T() string | |
} | |
type TesterI struct { | |
te string | |
} | |
func (t *TesterI) T() string { | |
return t.te | |
} | |
var ( | |
ti map[string]Tester | |
to map[string]*TesterI | |
) | |
func loadTiData() map[string]Tester { | |
ti = make(map[string]Tester) | |
for i := 0; i < 1000000; i++ { | |
t := &TesterI{te: fmt.Sprintf("t-%d", i)} | |
ti[t.te] = t | |
} | |
return ti | |
} | |
func loadToData() map[string]*TesterI { | |
to = make(map[string]*TesterI) | |
for i := 0; i < 1000000; i++ { | |
t := &TesterI{te: fmt.Sprintf("t-%d", i)} | |
to[t.te] = t | |
} | |
return to | |
} | |
func getTiCnt() int { | |
count := 0 | |
for _, v := range ti { | |
switch v.(type) { | |
case *TesterI: | |
count++ | |
} | |
} | |
return count | |
} | |
func getTiCnt2() int { | |
return len(ti) | |
} | |
func getToCnt() int { | |
count := 0 | |
for _ = range to { | |
count++ | |
} | |
return count | |
} | |
func getToCnt2() int { | |
return len(to) | |
} | |
func BenchmarkTi(b *testing.B) { | |
loadTiData() | |
b.ResetTimer() | |
b.SetBytes(20) | |
for i := 0; i < b.N; i++ { | |
getTiCnt() | |
} | |
} | |
func BenchmarkTi2(b *testing.B) { | |
loadTiData() | |
b.ResetTimer() | |
b.SetBytes(20) | |
for i := 0; i < b.N; i++ { | |
getTiCnt2() | |
} | |
} | |
func BenchmarkTo(b *testing.B) { | |
loadToData() | |
b.ResetTimer() | |
b.SetBytes(20) | |
for i := 0; i < b.N; i++ { | |
getToCnt() | |
} | |
} | |
func BenchmarkTo2(b *testing.B) { | |
loadToData() | |
b.ResetTimer() | |
b.SetBytes(20) | |
for i := 0; i < b.N; i++ { | |
getToCnt2() | |
} | |
} |
Author
barryz
commented
Jan 26, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment