非常慢的无序地图清除

时间:2014-01-09 08:12:34

标签: c++ windows

我有这个代码来优化从obj文件加载的网格:

PROFILE("Started optimizations.");
vector<float>* vs = new vector<float>();
unordered_map<string, int>* hm = new unordered_map<string, int>();
unordered_map<string, int>::const_iterator it;
UINT32 floatsInVertex = vertexSize / sizeof(float);
float* vertex = new float[floatsInVertex];
for (SIZE i = 0; i < nFaces; i++) {
    Face& face = faces[i];
    for (int j = 0; j < Face::FACE_SIZE; j++) {
        UINT32 index = face.indices_[j];
        UINT32 vInd = 0;
        UINT32 offset = index * vertexSize + posOffset;
        memcpy(&vertex[vInd++], &vertices[offset + 0], sizeof(float));
        memcpy(&vertex[vInd++], &vertices[offset + sizeof(float)], sizeof(float));
        memcpy(&vertex[vInd++], &vertices[offset + 2 * sizeof(float)], sizeof(float));
        if (face.hasNormals_) {
            index = face.normIndices_[j];
            offset = index * vertexSize + normOffset;
            memcpy(&vertex[vInd++], &vertices[offset + 0], sizeof(float));
            memcpy(&vertex[vInd++], &vertices[offset + sizeof(float)], sizeof(float));
            memcpy(&vertex[vInd++], &vertices[offset + 2 * sizeof(float)], sizeof(float));
        }
        if (face.hasUV_) {
            index = face.texIndices_[j];
            offset = index * vertexSize + uvOffset;
            memcpy(&vertex[vInd++], &vertices[offset + 0], sizeof(float));
            memcpy(&vertex[vInd++], &vertices[offset + sizeof(float)], sizeof(float));
        }
        string v(reinterpret_cast<const char*>(vertex), vertexSize);
        it = hm->find(v);
        if (hm->end() == it) {
            if (useShort) {
                indexArray->push_back(vs->size() / floatsInVertex);
            }
            else {
                indexArrayInt->push_back((UINT32) vs->size() / floatsInVertex);
            }
            hm->insert(pair<string, int>(v, vs->size() / floatsInVertex));
            for (SIZE i = 0; i < floatsInVertex; i++) {
                vs->push_back(vertex[i]);
            }
        }
        else {
            if (useShort) {
                indexArray->push_back(it->second);
            }
            else {
                indexArrayInt->push_back(it->second);
            }
        }
    }
}
PROFILE("Deleting hash map with size: %d.", hm->size());
LOGI("A");
delete hm;
LOGI("B");
PROFILE("Hash map deleted.");

〜8.5MB模型的结果是:

DEBUG: Started optimizations. Time: 0 (ms).
DEBUG: Deleting hash map with size: 194886. Time: 6879 (ms).
INFO: A
INFO: B
DEBUG: Hash map deleted. Time: 13046 (ms).

我的PROFILE功能只打印自上次PROFILE调用以来的时间。 如您所见,大小为194886的无序地图需要删除约7秒。调试版和发布版的时间大致相同。这是正常的吗?我该如何切断这段时间?

[编辑] Linux上的相同代码:

DEBUG: Started optimizations. Time: 0 (ms).
DEBUG: Deleting hash map with size: 194886. Time: 141(ms).
INFO: A
INFO: B
DEBUG: Hash map deleted. Time: 35(ms).

它的waaaaaaaaaaaay比Windows版本更快。

[EDIT2] 我的问题是:如何在Windows中加速此代码以匹配linux?

0 个答案:

没有答案