Last active
March 3, 2016 19:09
-
-
Save technoweenie/34671a1296ba375b477a to your computer and use it in GitHub Desktop.
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" | |
"sync" | |
) | |
func main() { | |
fmt.Println("WAT") | |
} | |
type Pointer struct { | |
Oid string | |
} | |
func filteredPointers(pointers []*Pointer, filter map[string]bool) []*Pointer { | |
filtered := make([]*Pointer, 0, len(pointers)) | |
for _, pointer := range pointers { | |
if filter[pointer.Oid] { | |
continue | |
} | |
filter[pointer.Oid] = true | |
filtered = append(filtered, pointer) | |
} | |
return filtered | |
} | |
func filterWithCallback(pointers []*Pointer, filter map[string]bool, cb func(*Pointer)) { | |
for _, pointer := range pointers { | |
if filter[pointer.Oid] { | |
continue | |
} | |
filter[pointer.Oid] = true | |
cb(pointer) | |
} | |
} | |
var mutex = &sync.Mutex{} | |
func filterFromChan(filter map[string]bool, input chan *Pointer) chan *Pointer { | |
output := make(chan *Pointer) | |
go func() { | |
for p := range input { | |
mutex.Lock() | |
if filter[p.Oid] { | |
mutex.Unlock() | |
continue | |
} | |
filter[p.Oid] = true | |
mutex.Unlock() | |
output <- p | |
} | |
close(output) | |
}() | |
return output | |
} |
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 ( | |
"crypto/sha1" | |
"encoding/hex" | |
"math/rand" | |
"strconv" | |
"testing" | |
) | |
var ( | |
pointers1 []*Pointer | |
pointers2 []*Pointer | |
) | |
func init() { | |
numPointers := 10000 | |
randNum := numPointers / 10 | |
pointers1 = make([]*Pointer, numPointers) | |
pointers2 = make([]*Pointer, numPointers) | |
for i := 0; i < numPointers; i++ { | |
pointers1[i] = newPointer(randNum) | |
pointers2[i] = newPointer(randNum * 2) | |
} | |
} | |
func BenchmarkFilterByList(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
f := make(map[string]bool) | |
filteredPointers(pointers1, f) | |
filteredPointers(pointers2, f) | |
} | |
} | |
func BenchmarkFilterByCallback(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
f := make(map[string]bool) | |
filtered := make([]*Pointer, 0, len(pointers1)) | |
cb := func(p *Pointer) { | |
filtered = append(filtered, p) | |
} | |
filterWithCallback(pointers1, f, cb) | |
filterWithCallback(pointers2, f, cb) | |
} | |
} | |
func BenchmarkFilterByChan(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
input := make(chan *Pointer) | |
f := make(map[string]bool) | |
out1 := filterFromChan(f, input) | |
out2 := filterFromChan(f, out1) | |
go func() { | |
for _, p := range pointers1 { | |
input <- p | |
} | |
for _, p := range pointers2 { | |
input <- p | |
} | |
close(input) | |
}() | |
filtered := make([]*Pointer, 0) | |
for p := range out2 { | |
filtered = append(filtered, p) | |
} | |
} | |
} | |
func newPointer(n int) *Pointer { | |
h := sha1.New() | |
h.Write([]byte(strconv.Itoa(rand.Intn(n)))) | |
return &Pointer{hex.EncodeToString(h.Sum(nil))} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment