大std :: vector的加速创建>> [

问题描述 投票:0回答:1
在我的国际象棋引擎代码库中,我使用了一个非常大的哈希表,哈希表的大小最多可以达到128 GB。hast表是由大小为4的存储桶组成的大型数组。使用STL std :: vector完成管理该大型表的代码。我对代码的性能感到满意,但是在初始化结构时遇到了一些问题。

哈希表的结构如下:

class ttEntry { private: signed int key:32; /*! 32 bit for the upper part of the key*/ signed int packedMove:16; /*! 16 bit for the move*/ signed int depth:16; /*! 16 bit for depth*/ signed int value:23; /*! 23 bit for the value*/ signed int generation:8; /*! 8 bit for the generation id*/ signed int staticValue:23; /*! 23 bit for the static evalutation (eval())*/ signed int type:3; /*! 2 bit for the type of the entry*/ /* 144 bits total = 16 bytes*/ public: explicit ttEntry(unsigned int _Key, Score _Value, unsigned char _Type, signed short int _Depth, unsigned short _Move, Score _StaticValue, unsigned char _gen): key(_Key), packedMove(_Move), depth(_Depth), value(_Value), generation(_gen), staticValue(_StaticValue), type(_Type){} explicit ttEntry(){} ... ... }; using ttCluster = std::array<ttEntry, 4>; class transpositionTable { private: std::vector<ttCluster> _table; .... .... }

我用于分配空间的代码如下:

uint64_t transpositionTable::setSize(unsigned long int mbSize) { uint64_t size = (uint64_t)((((uint64_t)mbSize) << 20) / sizeof(ttCluster)); _elements = size; _table.clear(); _table.shrink_to_fit(); try { _table.reserve(_elements); _table.resize(_elements); // big bottleneck } catch(...) { std::cerr << "Failed to allocate " << mbSize<< "MB for transposition table." << std::endl; exit(EXIT_FAILURE); } return _elements * 4; }

要以128GB的内存初始化表108秒。我对用已知值初始化内存不感兴趣,而只是分配空间并具有足够长的std :: vector。

我知道我可以用良好的旧C代码和malloc重写代码,但我想使用现代的std :: vector。

关于如何加速代码以及我在哪里做错的任何想法?

在我的国际象棋引擎代码库中,我使用了一个非常大的哈希表,哈希表的大小最多可以达到128 GB。 hast表是大小为4的存储桶的大型数组。使用...

c++ stdvector
1个回答
0
投票
为什么仍然需要调整大小?正如我所看到的,保留呼叫可以满足您的所有需求。它为特定大小分配内存。由于您想稍后对其进行初始化,因此只需使用push_back将数据写入其中即可。由于不必进行任何分配,因此与仅写入向量元素相比,push_back基本上不会降低性能。
© www.soinside.com 2019 - 2024. All rights reserved.