美文网首页c&c++
C++ STL中map/unordered_map/vector

C++ STL中map/unordered_map/vector

作者: NesTa_xP | 来源:发表于2017-07-04 16:03 被阅读0次
    • 测试总结
      待补充
    • 运行结果
    Paste_Image.png
    • 代码片段
      DWORD key = 0;
    
      auto nowClock = GetTickCount();
      vector<pair<DWORDLONG, FileInfo>> data;
      vector<SortItem> 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<DWORDLONG, FileInfo> 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<DWORDLONG, FileInfo> 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

        本文链接:https://www.haomeiwen.com/subject/aelxhxtx.html