c++ - Why is unordered_map and map giving the same performance? -


here code, unordered_map , map behaving same , taking same time execute. missing these data structures?

update: i've changed code based on below answers , comments. i've removed string operation reduce impact in profiling. measuring find() takes 40% of cpu in code. profile shows unordered_map 3 times faster, however, there other way make code faster?

#include <map> #include <unordered_map> #include <stdio.h>  struct property {     int a; };  int main() {     printf("performance summery:\n");     static const unsigned long num_iter = 999999;      std::unordered_map<int, property > myumap;     (int = 0; < 10000; i++) {         int ind = rand() % 1000;         property p;         p.a = i;         myumap.insert(std::pair<int, property> (ind, p));     }      clock_t tstart = clock();     (int = 0; < num_iter; i++) {         int ind = rand() % 1000;         std::unordered_map<int, property >::iterator itr = myumap.find(ind);     }      printf("time taken unordered_map: %.2fs\n", (double)(clock() - tstart)/clocks_per_sec);      std::map<int, property > mymap;     (int = 0; < 10000; i++) {         int ind = rand() % 1000;         property p;         p.a = i;         mymap.insert(std::pair<int, property> (ind, p));     }      tstart = clock();     (int = 0; < num_iter; i++) {         int ind = rand() % 1000;         std::map<int, property >::iterator itr = mymap.find(ind);     }      printf("time taken map: %.2fs\n", (double)(clock() - tstart)/clocks_per_sec); } 

the output here

performance summery: time taken unordered_map: 0.12s time taken map: 0.36s 

without going code, make few general comments.

  1. what measuring? profiling includes both populating , scanning data structures. given (presumably) populating ordered map take longer, measuring both works against idea of gains (or otherwise) of ordered map. figure out measuring , measure that.
  2. you have lot going on in code incidental profiling: there lot of object creation, string concatenation, etc etc. measuring. focus on profiling want measure (see point 1).
  3. 10,000 cases way small. @ scale other considerations can overwhelm measuring, particularly when measuring everything.

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -