1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#pragma warning(disable:4786)
#include <hash_map>
#include <unordered_map>
#include <map>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
struct eqstr
{
bool operator()(const string &s1, const string &s2) const
{
return s1==s2;
}
};
int main()
{
cout<<"Comparaison between hash_map, map, unordered_map"<<endl<<"(insertion of 12 elements, request find for 4 elements in each try)"<<endl;
cout<<"Number of tries?"<<endl;
int nb_tours=10000;
cin>>nb_tours;
system("PAUSE");
clock_t start, end_hash,end_map, end_unordered_map;
start = clock ();
hash_map<string, int, hash<string>, eqstr> months;
for(int i=nb_tours;i--;)
{
months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["june"] = 30;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;
int a =months["september"];
int b =months["april"];
int c =months["june"];
int d= months["august"];
a+=b;/*give some job to variables to cheat optimizer*/
c+=d;
a*=c;
}
end_hash = clock ();
cout <<" hashmap " <<(end_hash - start) / (double)CLOCKS_PER_SEC << endl;
start = clock ();
map<string, int, eqstr> mmonths;
for(int j=nb_tours;j--;)
{
mmonths["january"] = 31;
mmonths["february"] = 28;
mmonths["march"] = 31;
mmonths["april"] = 30;
mmonths["may"] = 31;
mmonths["june"] = 30;
mmonths["june"] = 30;
mmonths["august"] = 31;
mmonths["september"] = 30;
mmonths["october"] = 31;
mmonths["november"] = 30;
mmonths["december"] = 31;
int a =months["september"];
int b =months["april"];
int c =months["june"];
int d= months["august"];
a+=b;
c+=d;
a*=c;
}
end_map = clock ();
cout <<" map " <<(end_map - start) / (double)CLOCKS_PER_SEC << endl;
start = clock ();
unordered_map<string, int> un_mmonths;
for(int k=nb_tours;k--;)
{
un_mmonths["january"] = 31;
un_mmonths["february"] = 28;
un_mmonths["march"] = 31;
un_mmonths["april"] = 30;
un_mmonths["may"] = 31;
un_mmonths["june"] = 30;
un_mmonths["june"] = 30;
un_mmonths["august"] = 31;
un_mmonths["september"] = 30;
un_mmonths["october"] = 31;
un_mmonths["november"] = 30;
un_mmonths["december"] = 31;
int a =un_mmonths["september"];
int b =un_mmonths["april"];
int c =un_mmonths["june"];
int d= un_mmonths["august"];
a+=b;
c+=d;
a*=c;
}
end_unordered_map = clock ();
cout <<"unordered_map " <<(end_unordered_map - start) / (double)CLOCKS_PER_SEC << endl;
system("PAUSE");
return 0;
} |
Partager