我写了一个小程序,用两个样本数据创建了两百万张地图的向量,然后查询一些值。
我知道我可以在此时使用数据库,但是我只是在四处探索以实现性能优化。
代码:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <string>
#include <chrono>
using namespace std;
static int NUM_OF_MAPS = 2 * 1000 * 1000;
void buildVector(vector<unordered_map <string, int>> &maps);
void find(string key, int value, vector<unordered_map <string, int>> &maps);
int main() {
auto startPrg = chrono::steady_clock::now();
vector<unordered_map <string, int>> maps;
buildVector(maps);
for (int i = 0; i < 10; i++) {
string s(1, 'a'+ i);
find(s, i, maps);
}
auto endPrg = chrono::steady_clock::now();
cout << "program duration: " …Run Code Online (Sandbox Code Playgroud)