Created
April 12, 2023 22:17
-
-
Save inkel/a79f6e27b4b2cc7a4bc3ccdd182dbaa3 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 main | |
import ( | |
"strconv" | |
"testing" | |
) | |
func BenchmarkSetSpeed(b *testing.B) { | |
set := make(map[string]int) | |
for i := 0; i < 10; i++ { | |
set[strconv.Itoa(i)] = i | |
} | |
b.Run("explicit index", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
s := make([]string, len(set)) | |
j := 0 | |
for k := range set { | |
s[j] = k | |
j++ | |
} | |
SS = s | |
} | |
}) | |
b.Run("append len=0 cap=len(set)", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
s := make([]string, 0, len(set)) | |
for k := range set { | |
s = append(s, k) | |
} | |
SS = s | |
} | |
}) | |
b.Run("append len=0 without cap", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var s []string | |
for k := range set { | |
s = append(s, k) | |
} | |
SS = s | |
} | |
}) | |
} |
Author
inkel
commented
Apr 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment