Looking for tables in Go?

Would this be a suitable way to implement a lookup table in Go? Are there any better ways? I would like this to work if the entries turned out to be inconsistent.

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];

}

+4
source share
1 answer

You can use a flat slice if you want - then you just have null / 0 for records that don't have a record. This approach would be bad if your inconsistent values ​​can matter up to very high values.

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

I got these results after starting 100,000,000 indices on a fragment and a map: Map: 3062 ms Slice: 56 ms

, . .

: , , , , .

+1

Source: https://habr.com/ru/post/1532190/


All Articles