Created
August 21, 2014 01:19
-
-
Save hoffoo/41fb32efdf4c43421dc5 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 ( | |
"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