Lookup tables in Go?

huangapple go评论71阅读模式
英文:

Lookup tables in Go?

问题

这是在Go语言中实现查找表的一种适当方式吗?有没有更好的方法?如果条目是非连续的,我希望这个方法也能正常工作。

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

这种方式是一种常见的实现查找表的方式,使用了Go语言中的map数据结构。对于给定的val值,它会在rpMaxRegisters中查找对应的值并返回。这种方式适用于查找表中的条目是非连续的情况,因为map可以直接通过键来查找值,而不需要连续的索引。

如果你对性能有更高的要求,可以考虑使用数组来实现查找表,因为数组的访问速度通常比map更快。但是,使用数组的前提是查找表的索引是连续的,否则会浪费内存空间。在你的情况下,由于条目是非连续的,使用map是一个合理的选择。

总的来说,你的实现方式是可以的,如果没有特殊需求,这种方式已经足够好了。

英文:

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

}

答案1

得分: 2

你可以使用一个平面切片,如果你喜欢的话,对于没有条目的条目,你只需要使用null/0。如果你的非连续值可以范围到非常高的值,那么这种方法就不好了。

使用这段代码:http://play.golang.org/p/gLni-BzMKy

在一个切片和映射上运行了100,000,000个索引后,我得到了以下结果:
映射:3062毫秒 切片:56毫秒

话虽如此,在几乎任何实际应用中,速度差异都不应该有太大影响。我个人会选择使用映射。

编辑:我同意其他评论中的观点,建议在函数外部初始化映射,这样它只需要构建一次,而不是在每次调用时都构建。

英文:

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.

huangapple
  • 本文由 发表于 2014年3月18日 02:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/22462647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定