Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "testing"
- )
- func BenchmarkTestForeach(b *testing.B) {
- b.Run("slice", func(b *testing.B) {
- dataS := []string{
- "aaaaaaaa1111",
- "aaaaaaaa2222",
- "aaaaaaaa9999",
- "aaaaaaaa4444",
- "aaaaaaaa5555",
- "aaaaaaaa6666",
- "aaaaaaaa7777",
- "aaaaaaaa8888",
- "aaaaaaaa9999",
- }
- for i := 0; i < b.N; i++ {
- for _, v := range dataS {
- if v == "aaaaaaaa9999" {
- break
- }
- }
- }
- })
- b.Run("map", func(b *testing.B) {
- dataM := map[string]bool{
- "aaaaaaaa1111": true,
- "aaaaaaaa2222": true,
- "aaaaaaaa3333": true,
- "aaaaaaaa4444": true,
- "aaaaaaaa5555": true,
- "aaaaaaaa6666": true,
- "aaaaaaaa7777": true,
- "aaaaaaaa8888": true,
- "aaaaaaaa9999": true,
- }
- for i := 0; i < b.N; i++ {
- v, ok := dataM["aaaaaaaa9999"]
- if ok && v {
- continue
- }
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement