QHash storing large amount of data

爷,独闯天下 提交于 2019-12-24 00:45:58

问题


I've 10,000,000 entry of type struct{int, int, int, int}. when I store them using QHash or QMap, it occupies large amount of memory, indeed it must take about

10,000,000 * 4 * 4 (sizeof integer) <= 153 MB

but when I load my data it takes about 1.2 GB for both QHash and QMap, why this occurs and how can I optimize it for both speed and memory?(through any other data structure or some tricks to qmap and qhash)


回答1:


You've said in the comment that you are using another four ints as key - these values also have to be saved, so you are actually storing 8 ints, not 4. Apart from that, QHash has to store the value of the hash to efficiently lookup the values based on the key. The hash is an unsigned integer, so you've got 9 values, each 4 bytes long. It sums up to ~350 MB.

Also, internally QHash or QMap may use some padding between its elements, for example to satisfy data structure alignment requirements. Padding is a multiplier of 1 byte, which means that in case of 10 mln elements we may get at least several dozens of additional megabytes.

Besides, QHash and QMap are not just raw data - they both use additional pointers to their internal data structures etc., which is yet another reason why a single entry would take more space than you expected.

Another source of swollen data size might be the fact that for efficiency reasons, these classes may store some additional values so that they are precomputed when you call some of their methods.

Last but not least, QHash reserves more memory than its current elements need in any given moment for efficiency reasons (avoiding unnecessary copying). I would expect that the greater the size, the more memory it would reserve just in case, because copying gets more expensive. You can check the memory reserved in advance by calling the capacity() method. If you want to limit the amount of memory reserved, call the squeeze() method to tailor the memory so that it is just enough to contain the currently stored elements.



来源:https://stackoverflow.com/questions/44023855/qhash-storing-large-amount-of-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!