C++ STL中map/unordered_map/vector效率及内存消耗情况对比

  • 测试总结
    待补充
  • 运行结果
C++ STL中map/unordered_map/vector效率及内存消耗情况对比_第1张图片
Paste_Image.png
  • 代码片段
  DWORD key = 0;

  auto nowClock = GetTickCount();
  vector> data;
  vector itemdata;
  FileInfo file_info;

  HANDLE handle = GetCurrentProcess();
  PROCESS_MEMORY_COUNTERS pmc;
  GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
  ULONG mem_size = pmc.WorkingSetSize / 1024;
  
  for (DWORD i = 0; i < 10000000; i++) {
    int key = rand() % 100000;
    key += i;
    file_info.parent_fid = key;
    data.push_back(make_pair(key, file_info));
  }

  GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
  cout << "vector插入1000万条数据,耗时:" << (GetTickCount() - nowClock) << "ms" << " 消耗内存:" << pmc.WorkingSetSize / 1024 - mem_size << " K" << endl;
  mem_size = pmc.WorkingSetSize / 1024;

nowClock = GetTickCount();
  std::sort(data.begin(), data.end());
  cout << "vector排序1000万条数据,耗时:" << (GetTickCount() - nowClock) << "ms" << endl;

key = data[rand() % 900000].first;

  nowClock = GetTickCount();
  if (binary_search(data, key) > -1)
    cout << "vector从1000万条数据查找,耗时:" << (GetTickCount() - nowClock) << "ms" << endl;

  cout << "===========================================================" << endl;

  nowClock = GetTickCount();
  unordered_map unordered_map_data;
  for (DWORD i = 0; i < 10000000; i++) {
    file_info.parent_fid = key;
    unordered_map_data[i] = file_info;
  }
  GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
  cout << "unordered_map插入1000万条数据,耗时:" << (GetTickCount() - nowClock) << "ms" << " 消耗内存:" << pmc.WorkingSetSize / 1024 - mem_size << " K" << endl;
  mem_size = pmc.WorkingSetSize / 1024;

nowClock = GetTickCount();
  if (unordered_map_data.find(key) != unordered_map_data.end())
    cout << "unordered_map从1000万条数据查找,耗时:" << (GetTickCount() - nowClock) << "ms" << endl;

  cout << "===========================================================" << endl;

  nowClock = GetTickCount();
  map map_data;
  for (DWORD i = 0; i < 10000000; i++) {
    file_info.parent_fid = key;
    map_data[i] = file_info;
  }
  GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
  cout << "map插入1000万条数据,耗时:" << (GetTickCount() - nowClock) << "ms" << " 消耗内存:" << pmc.WorkingSetSize / 1024 - mem_size << " K" << endl;;
  mem_size = pmc.WorkingSetSize / 1024;

nowClock = GetTickCount();
  if (map_data.find(key) != map_data.end())
    cout << "map从1000万条数据查找,耗时:" << (GetTickCount() - nowClock) << "ms" << endl;

你可能感兴趣的:(C++ STL中map/unordered_map/vector效率及内存消耗情况对比)