5

Would this be an appropriate way to implement an lookup table in Go? Are there any better ways? I'd like this to work if the entries happened to be nonconsecutive.

func LookupRpMax(val uint8) float64 {
    rpMaxRegisters := map[uint8]float64 {
        0x00 : 3926991,
        0x01 : 3141593,
        0x02 : 2243995,
        0x03 : 1745329,
        0x04 : 1308997,
        0x05 : 981748,
        0x06 : 747998,
        0x07 : 581776,
        0x08 : 436332,
        0x09 : 349066,
        0x0A : 249333,
        0x0B : 193926,
        0x0C : 145444,
        0x0D : 109083,
        0x0E : 83111,
        0x0F : 64642,
        0x10 : 48481,
        0x11 : 38785,
        0x12 : 27704,
        0x13 : 21547,
        0x14 : 16160,
        0x15 : 12120,
        0x16 : 9235,
        0x17 : 7182,
        0x18 : 5387,
        0x19 : 4309,
        0x1A : 3078,
        0x1B : 2394,
        0x1C : 1796,
        0x1D : 1347,
        0x1E : 1026,
        0x1F : 798,
    }
    return rpMaxRegisters[val];

}

strobot
  • 157
  • 1
  • 6
  • At least declare and assign `rpMaxRegisters` outside the function. – Kavu Mar 17 '14 at 19:11
  • If you are familiar with Assembler you always could compare output of `go tool 6g -S` in different cases. In your current implementation Go will `CALL` `,runtime.makemap+0(SB)` every time `LookupRpMax` executes. – Kavu Mar 17 '14 at 19:26
  • I am having trouble declaring and assigning a map outside a function. `var m map[uint8]float64 = {0x00 : 32, 0x04 : 2,}` results in a `syntax error: unexpected {` error – strobot Mar 17 '14 at 19:31
  • You should use `var m = map[uint8]float{...}`, here is the example: http://play.golang.org/p/3ows0DCBXd. – Kavu Mar 17 '14 at 19:35
  • Thanks, that makes sense. I was getting confused about how Go declarations and initializations work. I agree about declaring it outside the function. I'm assuming this declaration will make the map local to the Go package that we are writing in? – strobot Mar 17 '14 at 19:42

1 Answers1

2

You can use a flat slice if you like - then you just have null/0 for entries that don't have a entry. That approach would be bad if your non-consecutive values can range to very high values.

Using this code: http://play.golang.org/p/gLni-BzMKy

I got these results after running 100,000,000 indexes on a slice and map: Map: 3062 ms Slice: 56 ms

That being said - the speed difference shouldn't matter in almost any real world use case. I would just use a map personally.

Edit: And I agree with the other comment that says to initialize the map outside the function so it only has to be constructed once and not on every call.

Ben Echols
  • 479
  • 2
  • 8
  • Thank you, this is informative. I assume there is no way to use something like C keyword "static" in the map declaration if is inside the function? – strobot Mar 17 '14 at 19:49
  • EDIT: Map can't be a const. Only primitives like float, int, string, etc. See: http://golang.org/ref/spec#Constants – Ben Echols Mar 17 '14 at 21:33