-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
111 lines (109 loc) · 3.2 KB
/
main.cpp
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Hashmap.h"
#include <unordered_map>
using namespace std;
long long generateRandLong(){
string str = "0000000000";
for(int i = 0; i < 10;i++){
char a = rand() % 10 + 48;
str[i] = a;
}
long long num = stoll(str);
return num;
}
bool testHashTable()
{
const int iters = 50000;
const int keysAmount = iters * 1;
// generate random keys:
long long* keys = new long long[keysAmount];
long long* keysToInsert = new long long[iters];
long long* keysToErase = new long long[iters];
long long* keysToFind = new long long[iters];
for (int i = 0; i < keysAmount; i++)
{
keys[i] = generateRandLong() ;
}
for (int i = 0; i < iters; i++)
{
keysToInsert[i] = keys[generateRandLong() % keysAmount];
keysToErase[i] = keys[generateRandLong() % keysAmount];
keysToFind[i] = keys[generateRandLong() % keysAmount];
}
// test my HashTable:
HashMap<long long> hashTable;
clock_t myStart = clock();
for (int i = 0; i < iters; i++)
{
hashTable.insert(keysToInsert[i],100);
/*cout << keysToInsert[i] << "\t";*/
}
/*cout << "\n";
cout << "\n";
hashTable.print();*/
int myInsertSize = hashTable.sizeH();
for (int i = 0; i < iters; i++)
{
hashTable.erase(keysToErase[i]);
}
int myEraseSize = hashTable.sizeH();
int myFoundAmount = 0;
for (int i = 0; i < iters; i++)
{
if (hashTable.find(keysToFind[i]) != NULL)
{
myFoundAmount++;
}
}
clock_t myEnd = clock();
float myTime = (float(myEnd - myStart)) / CLOCKS_PER_SEC;
// test STL hash table:
unordered_map<long long, int> unorderedMap;
clock_t stlStart = clock();
for (int i = 0; i < iters; i++)
{
unorderedMap.insert({keysToInsert[i], generateRandLong()});
}
int stlInsertSize = unorderedMap.size();
for (int i = 0; i < iters; i++)
{
unorderedMap.erase(keysToErase[i]);
}
int stlEraseSize = unorderedMap.size();
int stlFoundAmount = 0;
for (int i = 0; i < iters; i++)
{
if (unorderedMap.find(keysToFind[i]) != unorderedMap.end())
{
stlFoundAmount++;
}
}
clock_t stlEnd = clock();
float stlTime = (float(stlEnd - stlStart)) / CLOCKS_PER_SEC;
cout << "My HashTable:" << endl;
cout << "Time: " << myTime << ", size: " << myInsertSize << " - " << myEraseSize <<
", found amount: " << myFoundAmount << endl;
cout << "STL unordered_map:" << endl;
cout << "Time: " << stlTime << ", size: " << stlInsertSize << " - " << stlEraseSize
<< ", found amount: " << stlFoundAmount << endl << endl;
delete[] keys;
delete[] keysToInsert;
delete[] keysToErase;
delete[] keysToFind;
if (myInsertSize == stlInsertSize && myEraseSize == stlEraseSize && myFoundAmount ==
stlFoundAmount)
{
cout << "The lab is completed" << endl;
return true;
}
cerr << ":(" << endl;
return false;
}
int main() {
srand(time(NULL));
for(int i = 0;i<100;i++)
testHashTable();
return 0;
}