-
Notifications
You must be signed in to change notification settings - Fork 0
/
test7.c
104 lines (83 loc) · 2.4 KB
/
test7.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"
/* This program will fill an array of strings of size 100 with
* a bunch of names from the file "names3.txt".
* It will then search for a bunch of names using findItem.
* Each time it will report the number of read, write, malloc and free
* calls. Finally it searches for every name in the table and computes
* the average number of reads per search.
*/
#define CAPACITY 100 // total capacity of the array
#define STRLEN 50
void readFile( struct Performance *performance, struct Array *array )
{
FILE *fp;
char name[STRLEN];
char *retval;
fp = fopen( "names3.txt", "r" );
while (!feof(fp))
{
retval = fgets( name, STRLEN, fp );
if (retval!=NULL)
{
name[strlen(name)-1]='\0'; // replace \n with \0
writeItem( performance, array, array->nel, &name );
}
}
fclose( fp );
}
int cmp( const void *v1, const void *v2 )
{
const char *s1, *s2;
s1 = v1;
s2 = v2;
// printf( "cmp(\"%s\",\"%s\")\n", s1, s2 );
return strcmp(s1,s2);
}
int main( int argc, char **argv )
{
struct Performance *perf, *find_perf;
struct Array *array;
int i;
int test;
char *test_names[6] = { "Aguilar, Herbert",
"Dominguez, Keon",
"Juarez, Brielle",
"Reid, Lilly-May",
"Yu, Mahdi",
"Kremer, Stefan" };
char name[STRLEN];
// load the data into the array
perf = newPerformance();
array = newArray( perf, STRLEN, CAPACITY );
readFile( perf, array );
printf( "Read %d records\n", array->nel );
free( perf );
// run 6 tests and print the results
for (test=0;test<6;test++)
{
perf = newPerformance();
i=findItem( perf, array, &cmp, test_names[test] );
printf( "%s: i=%d\n", test_names[test], i );
printf( "reads: %5d\n", perf->reads );
printf( "writes: %5d\n", perf->writes );
printf( "mallocs: %5d\n", perf->mallocs );
printf( "frees: %5d\n", perf->frees );
free( perf );
printf( "\n" );
}
// run two separate counters (one just for find operations)
find_perf = newPerformance();
perf = newPerformance();
for (test=0;test<CAPACITY;test++)
{
readItem( perf, array, test, name );
i = findItem( find_perf, array, &cmp, name );
}
printf( "Average reads per find: %6.3f\n", (find_perf->reads)/(double)CAPACITY );
freeArray( perf, array );
free( perf );
free( find_perf );
}