Advertisement
Guest User

hash_map vs vector

a guest
Jan 22nd, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <vector>
  2. #include <hash_map>
  3. #include <time.h>
  4.  
  5. #define VECTOR_BASED    0
  6. #define HASHMAP_BASED   1
  7.  
  8. #define SAMPLE_COUNT    1000
  9.  
  10. #define METHOD_USED     HASHMAP_BASED
  11.  
  12. using namespace std;
  13.  
  14. int main(int argc, char** argv)
  15. {
  16. #if METHOD_USED == VECTOR_BASED
  17.     vector<int> val_tab(32);
  18. #elif METHOD_USED == HASHMAP_BASED
  19.     hash_map<int, int> val_tab;
  20. #else
  21.     int hash_map[32];
  22. #endif
  23.  
  24.     clock_t ins_min=99999,ins_max=0,ins_avg=0;
  25.     clock_t out_min=99999,out_max=0,out_avg=0;
  26.  
  27.     clock_t start;
  28.  
  29.     printf("sampling insertion time n=%u.....", SAMPLE_COUNT);
  30.     for(int i=0; i<SAMPLE_COUNT; i++)
  31.     {
  32.         start=clock();
  33.  
  34.         val_tab[9]=0;
  35.         val_tab[31]=-12;
  36.         val_tab[24]=23;
  37.  
  38.         start=clock()-start;
  39.  
  40.         ins_max=start>ins_max?start:ins_max;
  41.         ins_min=start<ins_min?start:ins_min;
  42.         ins_avg+=start;
  43.     }
  44.     printf("done.\n");
  45.  
  46.     printf("sampling output time n=%u.....", SAMPLE_COUNT);
  47.     for(int c=0; c<SAMPLE_COUNT; c++)
  48.     {
  49.         start=clock();
  50.         for(int i=0; i<32; i++)
  51.         {
  52.             val_tab[i]=val_tab[i]*23;
  53.         }
  54.         start=clock()-start;
  55.  
  56.         out_max=start>out_max?start:out_max;
  57.         out_min=start<out_min?start:out_min;
  58.         out_avg+=start;
  59.     }
  60.     printf("done.\n");
  61.     printf("\n");
  62.    
  63.     printf("insertion time: min=%u ms, max=%u ms, avg=%.4f ms\n", ins_min, ins_max, ins_avg*0.001);
  64.     printf("output time: min=%u ms, max=%u ms, avg=%.4f ms\n", out_min, out_max, out_avg*0.001);
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement