On 2013/3/12 Julian Orth <
ju....@gmail.com> wrote:
> In MarkedCache1 I check if the cache reached the full size, and if unmared
> == 0, then I iterate over all keys and set the marked field to false. In
> either case I then use range to find the first element which is unmarked and
> delete it. Then the new key is added.
> MarkedCache3 is implemented in the obvious way. I use range here too.
> MarkedCache4 uses separately stored keys to get random unmarked elements via
> a random number generator.
> MarkedCache7 goes further and creates an efficient bijection between natural
> numbers and the stored keys. This allows me to store the cached values in a
> slice. This means that I don't have to use the keys when I search for
> unmarked elements.
>
> Here are the Set functions:
>
> MC1:
http://pastebin.com/uAsjQXZB
> MC3:
http://pastebin.com/eCHpbi1Z
Breaking after the first iteration key is a bad idea. Don't do that.
It's not only badly random, it's just so unrandom you want to get away
from that.
I have tried and failed, and it had very, very nasty effects on production.
Running the below program a few times (go tip on linux/amd64) I get this:
map[2:130 3:99100 1:102 4:128 5:540]
map[4:99103 5:132 1:503 2:135 3:127]
map[1:256 2:129 3:111 4:99378 5:126]
I am pretty sure it was discussed already but can't find the discussion.
// PROGRAM BEGINS HERE
package main
import "fmt"
type T map[string]int
func main() {
m := T{"1": 1, "2": 1, "3": 1, "4": 1, "5": 1}
count := make(T)
for i := 0; i < 100000; i++ {
for k := range m {
count[k]++
break
}
}
fmt.Printf("%+v", count)
}