-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.c
111 lines (88 loc) · 2.65 KB
/
perf.c
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include "hash.h"
#define LENGTH 100
#define BILLION 1000000000L
char* GenerateRandomString(char* str) {
int i;
srand((unsigned)time(NULL));
for ( i = 0 ; i < LENGTH - 1 ; i++ ) *str = 'A' + (char)(rand() % 20);
str[LENGTH]='\0';
return str;
}
int main () {
hash_table hash_array;
hash_table* hash_info = &hash_array;
hash_info->size = 256;
CreateHash(hash_info);
struct timespec start, stop;
double worktime;
// test AddElement
unsigned long j;
char tmp[LENGTH] = {0};
if (clock_gettime( CLOCK_REALTIME, &start) == -1) {
perror ("clock gettime");
return -1;
}
for ( j = 0; j < 10000; ++j ) {
AddElement(GenerateRandomString(tmp), hash_info);
}
if (clock_gettime( CLOCK_REALTIME, &stop) == -1) {
perror ("clock gettime");
return -1;
}
worktime = ((( stop.tv_sec - start.tv_sec )*1e9 + ( stop.tv_nsec - start.tv_nsec ))*1e-9)/10000;
printf ("%.10f sec for AddElement cycle \n", worktime);
// test SearchElement
if (clock_gettime( CLOCK_REALTIME, &start) == -1) {
perror ("clock gettime");
return -1;
}
for ( j = 0; j < 10000; ++j ) {
SearchElement(GenerateRandomString(tmp), hash_info);
}
if (clock_gettime( CLOCK_REALTIME, &stop) == -1) {
perror( "clock gettime" );
return -1;
}
worktime = ((( stop.tv_sec - start.tv_sec )*1e9 + ( stop.tv_nsec - start.tv_nsec ))*1e-9)/10000;
printf("%.10f sec for SearchElement cycle \n", worktime);
// test Remove
if (clock_gettime( CLOCK_REALTIME, &start) == -1) {
perror( "clock gettime" );
return -1;
}
int k;
for ( j = 0; j < 10000; ++j ) {
k = Remove(GenerateRandomString(tmp), hash_info);
}
if (clock_gettime( CLOCK_REALTIME, &stop) == -1) {
perror( "clock gettime" );
return -1;
}
worktime = ((( stop.tv_sec - start.tv_sec )*1e9 + ( stop.tv_nsec - start.tv_nsec ))*1e-9)/10000;
printf ("%.10f sec for Remove cycle \n", worktime);
// test CleanMemory
if (clock_gettime( CLOCK_REALTIME, &start) == -1) {
perror ("clock gettime");
return -1;
}
for ( j = 0; j < 10000; ++j ) {
hash_table hash_array;
hash_table* hash_info = &hash_array;
hash_info->size = 256;
CreateHash(hash_info);
CleanMemory(hash_info);
}
if (clock_gettime( CLOCK_REALTIME, &stop) == -1) {
perror( "clock gettime" );
return -1;
}
worktime = ((( stop.tv_sec - start.tv_sec )*1e9 + ( stop.tv_nsec - start.tv_nsec ))*1e-9)/10000;
printf ("%.10f sec for CreateHash and CleanMemory cycle\n", worktime);
return 0;
}