Created
March 25, 2023 07:52
-
-
Save C-Pro/d7018fc12ad31f08f890e72e5ffec32a to your computer and use it in GitHub Desktop.
Compare allocation size for different type of map values for "set" type containers, where only the keys are of any interest.
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_test | |
import "testing" | |
func BenchmarkBoolMap(b *testing.B) { | |
m := map[int]bool{} | |
for i := 0; i < b.N; i++ { | |
m[i] = true | |
} | |
} | |
func BenchmarkEmptyStructMap(b *testing.B) { | |
m := map[int]struct{}{} | |
for i := 0; i < b.N; i++ { | |
m[i] = struct{}{} | |
} | |
} | |
func BenchmarkEmptyInterfaceMap(b *testing.B) { | |
m := map[int]interface{}{} | |
for i := 0; i < b.N; i++ { | |
m[i] = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Empty struct is the winner: