fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <algorithm>
  6. #include <chrono>
  7. using namespace std::chrono;
  8. using namespace std;
  9.  
  10. std::map<std::string,int> cache;
  11.  
  12. int compute_new(int old) {
  13. return old+1;
  14. }
  15.  
  16. void fun_with_ref(const std::string& key) {
  17. int& int_ref = cache[key];
  18. int_ref = compute_new(int_ref);
  19. }
  20.  
  21. void fun_with_val(const std::string& key) {
  22. int int_val = cache[key];
  23. cache[key] = compute_new(int_val);
  24. }
  25.  
  26. int main() {
  27. vector<string> keys;
  28. for (int i = 0 ; i != 1000000 ; i++) {
  29. auto key = to_string(i);
  30. cache[key] = i;
  31. keys.push_back(key);
  32. }
  33. auto ms1 = duration_cast< milliseconds >(
  34. system_clock::now().time_since_epoch()
  35. ).count();
  36. for (const string& key : keys) {
  37. fun_with_ref(key);
  38. }
  39. auto ms2 = duration_cast< milliseconds >(
  40. system_clock::now().time_since_epoch()
  41. ).count();
  42. for (const string& key : keys) {
  43. fun_with_val(key);
  44. }
  45. auto ms3 = duration_cast< milliseconds >(
  46. system_clock::now().time_since_epoch()
  47. ).count();
  48. cout << "Ref: " << (ms2-ms1) << endl;
  49. cout << "Val: " << (ms3-ms2) << endl;
  50. }
Success #stdin #stdout 3.06s 58088KB
stdin
Standard input is empty
stdout
Ref: 557
Val: 1064