如果使用预定义的地图,重建项目会花费相当长的时间吗?

时间:2014-08-16 02:57:25

标签: c++ visual-studio visual-c++

编译/链接设置相当正常(/ O2用于优化,/ LTCG用于链接器 - 由于使用了使用/ GL编译的模块,因此强制执行此操作。)

我发现代码段导致编译时间增加,但我不知道如何解决问题。尽管编译时间很长,但当前代码在最终完成时确实可以正常工作。

当删除以下功能时,编译时间从125秒减少到4秒(几乎所有时间增益都处于链接阶段):

static void InitializeItemSlotLists(std::map<uint32_t, uint32_t>& HEAD_IDS, std::map<uint32_t, uint32_t>& SHOULDER_IDS, std::map<uint32_t, uint32_t>& CHEST_IDS, std::map<uint32_t, uint32_t>& GLOVE_IDS, std::map<uint32_t, uint32_t>& WAIST_IDS, std::map<uint32_t, uint32_t>& LEGS_IDS, std::map<uint32_t, uint32_t>& FEET_IDS, std::map<uint32_t, uint32_t>& WEAPON_1H_IDS, std::map<uint32_t, uint32_t>& WEAPON_2H_IDS, std::map<uint32_t, uint32_t>& SHIELD_IDS, std::map<uint32_t, uint32_t>& OFFHAND_IDS) {
    HEAD_IDS[60202] = 93792;
    HEAD_IDS[60222] = 35113;
    HEAD_IDS[60237] = 81960;
    HEAD_IDS[60243] = 77144;
    HEAD_IDS[60249] = 91486;
    HEAD_IDS[60256] = 91255;
    HEAD_IDS[60258] = 82002;
    HEAD_IDS[60299] = 82025;
    (This continues for 5000 or so more lines.)
}

我可以在运行时从外部文本文件加载所有这些,但老实说,这只会增加无意义的复杂性。尽管数据显得多么随意,但未来几乎不可能改变。

是否正在尝试优化这一大块数据?如果是这样,有没有办法可以强制它忽略这个特定的文件进行优化(它在自己的.cpp文件中有自己的标题来预定义函数)?

1 个答案:

答案 0 :(得分:3)

如果您必须使用静态数据执行此操作,并且您不希望/不能使用C ++ 11提供的map brace-initializer,那么可以避免性能损失。尝试优化5000行std :: map赋值的编译器是将所有数据放入静态数组中,如下所示:

#include <map>
#include <stdint.h>

typedef std::map<uint32_t, uint32_t> IdMap;

struct IdRow {
  uint32_t src;
  uint32_t dst;
};

IdRow InitialHeadIds[] = {
  { 60202, 93792 },
  { 60222, 35113 },
  { 60237, 81960 },
  { 60243, 77144 },
  { 60249, 91486 },
  { 60256, 91255 },
  { 60258, 82002 },
  { 60299, 82025 }
};

static void InitializeItemSlotLists(
    IdMap& HEAD_IDS, IdMap& SHOULDER_IDS, IdMap& CHEST_IDS, IdMap& GLOVE_IDS,
    IdMap& WAIST_IDS, IdMap& LEGS_IDS, IdMap& FEET_IDS, IdMap& WEAPON_1H_IDS,
    IdMap& WEAPON_2H_IDS, IdMap& SHIELD_IDS, IdMap& OFFHAND_IDS) {
  for (int i = 0; i < sizeof(InitialHeadIds) / sizeof(IdRow); i++) {
    IdRow curr = InitialHeadIds[i];
    HEAD_IDS[curr.src] = curr.dst;
  }
}

int main() {
  IdMap headIds, otherIds;
  InitializeItemSlotLists(
    headIds, otherIds, otherIds, otherIds, otherIds, otherIds, otherIds,
    otherIds, otherIds, otherIds, otherIds);
  return 0;
}