home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
progmisc
/
euphor10.zip
/
DATABASE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-28
|
4KB
|
162 lines
/* C version of database update benchmark */
#define ITERATIONS 50000
#include <stdio.h>
#include <time.h>
char *realloc();
char *malloc();
/* update commands */
#define NEW 1 /* add a new account */
#define UPDATE 2 /* add/substract from their account */
#define DELETE 3 /* delete someone */
struct rd {
char *name;
double amount;
int code;
} raw_data[] = {
{"George Bush", 1000, NEW},
{"Bill Clinton", 2000, NEW},
{"Brian Mulroney", 500, NEW},
{"Ross Perot", 10000, NEW},
{"Ross Perot", 0, DELETE},
{"George Bush", -30.55, UPDATE},
{"Madonna", 2500, NEW},
{"Boris Yeltsin", 100, NEW},
{"Michael Jackson", 50, NEW},
{"Peter Mansbridge", 1200, NEW},
{"Bill Clinton", +500, UPDATE},
{"Rod Stewart", 3000, NEW},
{"Boris Yeltsin", 0, DELETE},
{"Sharon Stone", 1500, NEW},
{"Clint Eastwood", 1900, NEW},
{"Madonna", 0, DELETE },
{"Sally Jessy Raphael", 750, NEW},
{"Brian Mulroney", -400, DELETE},
{"Richard Gere", 299, NEW},
{"Rod Stewart", 0, DELETE},
{"Demi Moore", 350, NEW},
{"Bruce Willis", 480, NEW},
{"Sharon Stone", +900.50, UPDATE},
{"Arsenio Hall", 300, NEW},
{"David Letterman", 450, NEW},
{"Whoopi Goldberg", 1050, NEW},
{"Clint Eastwood", +2500, UPDATE},
{"Michael Jackson", -50, UPDATE},
{"Clint Eastwood", 0, DELETE},
{"Jack Nicholson", 3000, NEW}
};
/* The database */
struct db_rec {
char *name; /* person's name */
double amount; /* account balance */
} *database = NULL;
int size = 0; /* current database size */
dump()
/* used to verify that program works correctly
not part of timing loop */
{
int i;
for (i = 0; i < size; i++) {
printf("%20s: %8.2f\n", database[i].name, database[i].amount);
}
}
purge()
/* empty the database - free all storage */
{
int i;
if (database != NULL) {
for (i = 0; i < size; i++)
free(database[i].name);
free(database);
}
size = 0;
database = NULL;
}
Out_Of_Space()
{
fprintf(stderr, "out of space\n");
exit(1);
}
update(struct rd *data_stream, int nitems)
{
int i, action, account_no;
char *name_copy;
struct db_rec *new_database;
int j, k;
char *pname;
struct rd *record;
for (i = 0; i < nitems; i++) {
record = &data_stream[i];
action = record->code;
if (action == NEW) {
size++;
database = (struct db_rec *)realloc(database,
size * sizeof(struct db_rec));
if (database == NULL)
Out_Of_Space();
name_copy = (char *)malloc(strlen(record->name)+1);
if (name_copy == NULL)
Out_Of_Space();
strcpy(name_copy, record->name);
database[size-1].name = name_copy;
database[size-1].amount = record->amount;
}
else {
pname = data_stream[i].name;
for (account_no = 0; account_no < size; account_no++) {
if (strcmp(pname, database[account_no].name) == 0)
break;
}
if (action == UPDATE) {
database[account_no].amount += record->amount;
}
else if (action == DELETE) {
size--;
new_database = (struct db_rec *)malloc(size *
sizeof(struct db_rec));
if (new_database == NULL)
Out_Of_Space();
j = 0;
for (k = 0; k < account_no; k++) {
new_database[j] = database[k];
j++;
}
for (k = account_no+1; k <= size; k++) {
new_database[j] = database[k];
j++;
}
free(database);
database = new_database;
}
}
}
}
main()
{
long i, t;
t = clock();
for (i = 1; i <= ITERATIONS; i++) {
purge();
update(&raw_data, sizeof(raw_data)/sizeof(struct rd));
}
printf("%d iterations in %.2f seconds\n", ITERATIONS,
(clock() - t)/(double)CLOCKS_PER_SEC);
dump();
}