Alien-Judy
view release on metacpan or search on metacpan
src/judy-1.0.5/test/SLcompare.c view on Meta::CPAN
wcreate(char *word, TREEREC * par)
{
TREEREC *tmp;
tmp = (TREEREC *) malloc(sizeof(TREEREC));
tmp->word = (char *)malloc(strlen(word) + 1);
strcpy(tmp->word, word);
tmp->left = tmp->right = NULL;
tmp->par = par;
return (tmp);
}
#define INITARRAY ANSREC ans = {0}
#define STORESTRING(STRING) redblackinsert(&ans, STRING)
#define GETSTRING(STRING) redblacksearch(&ans, STRING)
#endif // REDBLACKMETHOD
//=======================================================================
// M A I N P R O G R A M -by- Doug Baskins
//=======================================================================
// error routine for system routines for accessing file
#define FILERROR \
{ \
printf("%s: Cannot open file \"%s\": %s " \
"(errno = %d)\n", argv[0], argv[1], strerror(errno), \
errno); \
fprintf(stderr, "%s: Cannot open file \"%s\": %s " \
"(errno = %d)\n", argv[0], argv[1], strerror(errno), \
errno); \
exit(1); \
}
//=======================================================================
// M E A S U R E A D T S P E E D and M E M O R Y U S A G E
//=======================================================================
int
main(int argc, char *argv[])
{
TIMER_vars(tm); // declare timer variables
int fd; // to read file.
struct stat statbuf; // to get size of file
char *Pfile; // ram address of file
size_t fsize; // file size in bytes
char *FSmap; // start address of mapped file
char *FEmap; // end address+1 of mapped file
char String[MAXLINE]; // input buffer
int StrCnt; // line counter
int ii; // temp
INITARRAY;
// CHECK FOR REQUIRED INPUT FILE PARAMETER:
if (argc != 2)
{
printf("Usage: %s <text file>\n", argv[0]);
exit(1);
}
// GET FILE SIZE
if (stat(argv[1], &statbuf) == -1)
FILERROR;
fsize = statbuf.st_size;
// OPEN INPUT FILE:
if ((fd = open(argv[1], O_RDONLY)) == -1)
FILERROR;
// MEMORY MAP FILE
Pfile =
(char *)mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (Pfile == (char *)-1)
FILERROR;
FEmap = Pfile + fsize; // set end+1 address
// print command name + run arguments
printf("# ");
for (ii = 0; ii < argc; ii++)
printf("%s ", argv[ii]);
printf("\n");
fflush(stdout);
// file is in RAM, read again to measure time
STARTTm(tm); // start timer
for (StrCnt = 0, FSmap = Pfile; FSmap < FEmap; )
{
GETLINE(String, FSmap); // 'read' next string
StrCnt++;
}
ENDTm(DeltaUSec, tm); // end timer
//
// print header - 6 fields
printf("# lines avg_linelen getline stored");
printf(" RAMused/line store/ln lookup/ln ADT\n");
// print numb lines filesize/line
printf("%8u", StrCnt); // Number of lines
printf(" %6.1f byts", (double)fsize / (double)StrCnt); // filesize/line
fflush(stdout);
// print time to read a line
printf(" %5.3f uS", DeltaUSec / (double)StrCnt);
fflush(stdout);
// INPUT/STORE LOOP:
// read each input line into String and store into ADT
STARTmem; // current malloc() mem usage
STARTTm(tm); // start timer
for (FSmap = Pfile; FSmap < FEmap; )
{
GETLINE(String, FSmap); // 'read' next string
STORESTRING(String);
}
ENDTm(DeltaUSec, tm); // end timer
ENDmem; // current malloc() mem usage
// print number of non-duplicate lines
printf(" %8u", StrCnt - gDupCnt); // duplicate lines
// print RAM used by malloc() by ADT to store data
printf(" %7.1f byts", (double)(DeltaMem + MEMOVD) /
(double)(StrCnt - gDupCnt));
// print time per line to store the data
printf(" %6.3f uS", DeltaUSec / (double)StrCnt);
fflush(stdout);
// READ BACK LOOP
STARTTm(tm); // start timer
for (FSmap = Pfile; FSmap < FEmap; )
{
GETLINE(String, FSmap); // 'read' next string
GETSTRING(String);
}
ENDTm(DeltaUSec, tm); // end timer
// print time per line to lookup the data from the ADT
printf(" %6.3f uS", DeltaUSec / (double)StrCnt); // store time/line
printf(" %s\n", ADTMETHOD);
return (0); // done
} // main()
( run in 0.653 second using v1.01-cache-2.11-cpan-13bb782fe5a )