-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionSix.h
126 lines (105 loc) · 2.58 KB
/
FunctionSix.h
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
116
117
118
119
120
121
122
123
124
125
126
#ifndef FUNCTIONSIX_H
#define FUNCTIONSIX_H
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cstdlib>
using namespace std;
struct UserSortTxt {
int user_id;
string user_password;
};
int ReadByUserTxt(UserSortTxt array[]) {
ifstream readIt("./FunctionFiveTxt/HeapSort.txt");
if (readIt.fail()) {
cout << "该文件没有找到!" << endl;
cout << "程序已经退出!" << endl;
exit(1);
}
int num = 0;
while (!readIt.eof()) {
readIt >> array[num].user_id >> array[num].user_password;
num++;
if (readIt.eof()) break;
}
readIt.close();
return num;
}
void GetRandomNumFor2000In6(long long int array[]) {
srand(time(NULL));
for (int i = 0; i < 2000; i++) {
array[i] = (rand() * rand()) % 1230000;
}
}
void GetRandomNumFor20In6(long long int array[]) {
for (int i = 0; i < 20; i++) {
long long int num = rand() * rand();
if (num > 1230000) {
array[i] = num;
}
}
}
bool Find(UserSortTxt x, UserSortTxt array[],int size) {
int low = 0;
int length = size;
while (low <= length) {
int middle = (low + length) / 2;
if (x.user_id == array[middle].user_id) {
return true;
}
else if (x.user_id < array[middle].user_id) {
length = middle - 1;
}
else if (x.user_id > array[middle].user_id) {
low = middle + 1;
}
}
return false;
}
double TestCaseFor2000RandomNumIn6() {
long long int array[2003];
GetRandomNumFor2000In6(array);
UserSortTxt* arrayTxt = new UserSortTxt[1500000];
int size = ReadByUserTxt(arrayTxt);
UserSortTxt txt;
clock_t start = clock();
for (int i = 0; i < 2000; i++) {
txt.user_id = array[i];
Find(txt, arrayTxt, size);
}
clock_t end = clock();
double useTime = (double)(end - start) / CLOCKS_PER_SEC;
return useTime;
}
double TestCaseFor20RandomNumIn6() {
long long int array[23];
GetRandomNumFor20In6(array);
UserSortTxt* arrayTxt = new UserSortTxt[1500000];
int size = ReadByUserTxt(arrayTxt);
UserSortTxt txt;
clock_t start = clock();
for (int i = 0; i < 20; i++) {
txt.user_id = array[i];
Find(txt, arrayTxt, size);
}
clock_t end = clock();
double useTime = (double)(end - start) / CLOCKS_PER_SEC;
return useTime;
}
void TestCaseForFunctionSix() {
double time1;
double time2;
double useTime;
time1 = TestCaseFor2000RandomNumIn6();
time2 = TestCaseFor20RandomNumIn6();
useTime = time1 + time2;
cout << endl;
cout << "The function six need time is : " << useTime << "s" << endl;
cout << endl;
}
#endif