#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <chrono>
using namespace std::chrono;
using namespace std;
std::map<std::string,int> cache;
int compute_new(int old) {
return old+1;
}
void fun_with_ref(const std::string& key) {
int& int_ref = cache[key];
int_ref = compute_new(int_ref);
}
void fun_with_val(const std::string& key) {
int int_val = cache[key];
cache[key] = compute_new(int_val);
}
int main() {
vector<string> keys;
for (int i = 0 ; i != 1000000 ; i++) {
auto key = to_string(i);
cache[key] = i;
keys.push_back(key);
}
auto ms1 = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count();
for (const string& key : keys) {
fun_with_ref(key);
}
auto ms2 = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count();
for (const string& key : keys) {
fun_with_val(key);
}
auto ms3 = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count();
cout << "Ref: " << (ms2-ms1) << endl;
cout << "Val: " << (ms3-ms2) << endl;
}