Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bench
- import (
- "fmt"
- "strings"
- "testing"
- )
- func GenMap(count int) map[string]string {
- xrange := make([]struct{}, count)
- res := make(map[string]string, count)
- for i := range xrange {
- key := fmt.Sprintf("key:%d", i)
- value := fmt.Sprintf("value:%d", i)
- res[key] = value
- }
- return res
- }
- func SlowLabels(m map[string]string) string {
- strs := []string{}
- for k, v := range m {
- strs = append(strs, k+"="+v)
- }
- return strings.Join(strs, ", ")
- }
- func FastLabels(m map[string]string) string {
- strs := make([]string, 0, len(m))
- for k, v := range m {
- strs = append(strs, k+"="+v)
- }
- return strings.Join(strs, ", ")
- }
- func Benchmark100S(b *testing.B) {
- input := GenMap(100)
- for n := 0; n < b.N; n++ {
- SlowLabels(input)
- }
- }
- func Benchmark100F(b *testing.B) {
- input := GenMap(100)
- for n := 0; n < b.N; n++ {
- FastLabels(input)
- }
- }
- func Benchmark10S(b *testing.B) {
- input := GenMap(10)
- for n := 0; n < b.N; n++ {
- SlowLabels(input)
- }
- }
- func Benchmark10F(b *testing.B) {
- input := GenMap(10)
- for n := 0; n < b.N; n++ {
- FastLabels(input)
- }
- }
- func Benchmark5S(b *testing.B) {
- input := GenMap(5)
- for n := 0; n < b.N; n++ {
- SlowLabels(input)
- }
- }
- func Benchmark5F(b *testing.B) {
- input := GenMap(5)
- for n := 0; n < b.N; n++ {
- FastLabels(input)
- }
- }
- func Benchmark3S(b *testing.B) {
- input := GenMap(3)
- for n := 0; n < b.N; n++ {
- SlowLabels(input)
- }
- }
- func Benchmark3F(b *testing.B) {
- input := GenMap(3)
- for n := 0; n < b.N; n++ {
- FastLabels(input)
- }
- }
- func Benchmark1S(b *testing.B) {
- input := GenMap(1)
- for n := 0; n < b.N; n++ {
- SlowLabels(input)
- }
- }
- func Benchmark1F(b *testing.B) {
- input := GenMap(1)
- for n := 0; n < b.N; n++ {
- FastLabels(input)
- }
- }
- /*
- Benchmark100S 50000 30674 ns/op
- Benchmark100F 50000 26230 ns/op
- Benchmark10S 300000 4226 ns/op
- Benchmark10F 500000 2911 ns/op
- Benchmark5S 1000000 2123 ns/op
- Benchmark5F 1000000 1554 ns/op
- Benchmark3S 1000000 1423 ns/op
- Benchmark3F 1000000 1048 ns/op
- Benchmark1S 5000000 334 ns/op
- Benchmark1F 5000000 302 ns/op
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement