Skip to content

Instantly share code, notes, and snippets.

@hoffoo
Created August 21, 2014 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffoo/41fb32efdf4c43421dc5 to your computer and use it in GitHub Desktop.
Save hoffoo/41fb32efdf4c43421dc5 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
func main() {
nums := make([]uint16, 10000)
for i, _ := range nums {
nums[i] = uint16(rand.Uint32())
}
slowcount := map[uint16]int{}
fastcount := make([]int, int(math.Pow(2, 16)))
start := time.Now()
for _, n := range nums {
//printBits(n)
if count, ok := slowcount[n]; ok {
slowcount[n] = count + 1
} else {
slowcount[n] = 1
}
}
fmt.Printf("slowcount: %d\n", time.Now().UnixNano()-start.UnixNano())
start = time.Now()
for _, n := range nums {
fastcount[n]++
}
fmt.Printf("fastcount: %d\n", time.Now().UnixNano()-start.UnixNano())
for i, _ := range fastcount {
if fastcount[i] != slowcount[uint16(i)] {
fmt.Printf("WRONT COUNT\n")
}
}
fmt.Printf("done\n")
}
func printBits(n int16) {
for i := int16(128); i > 0; i >>= 1 {
if i&n == i {
fmt.Print(1)
} else {
fmt.Print(0)
}
}
fmt.Println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment