donmqryh49993 2017-03-16 03:09
浏览 52

我的hashmap实现的性能改进

I decided i'd try make my own hashmap (here)

For reads it's 28% slower than the standard library implementation, I'm wondering if it's possible to speed up the following code, Index() which is critical for lookups:

const numOnes = uint8(20)
const ones = uint32(1 << numOnes - 1)

func (m *Map) Index(num uint64) uint32 {
    part := uint32(num) & ones
    remaining := num >> numOnes
    start := m.starts[part]
    bitsNum := m.bitNums[part]
    matchedBits := bitsNum & uint16(remaining)
    offset := BitScoreCache[bitsNum][matchedBits]
    return start + uint32(offset)
}

note BitScoreCache is var BitScoreCache [5000][5000]uint16 which is supposed to be readonly and be shared between multiple different map instances.

example usage:

func (pa PrimeAdvancedAnagrammar) GetAnagrams(word string) []string {
    return pa.m[pa.locator.Index(PrimeProduct(word))] //pa.m is an [][]string
}

versus standard library:

func (pba PrimeBasicAnagrammar) GetAnagrams(word string) []string {
    return pba.m[PrimeProduct(word)] //pba.m is a map[uint64][]string
}

What are the main reasons it's slower than the standard library for so few operations?

  • 写回答

1条回答 默认 最新

  • doubi6898 2017-03-21 21:53
    关注

    Combining the two arrays into one array of structs reduced cache misses and was the biggest performance improvement.

    also returning early on the case that there is no collisions improved performance.

    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类