Commit 7a96693c authored by Geoff Simmons's avatar Geoff Simmons

Add PH_Stats().

parent 43e4b858
......@@ -437,6 +437,38 @@ PH_Dump(struct ph *ph, char **strings)
return (sb);
}
void
PH_Stats(const struct ph * const restrict ph,
char * const restrict * const restrict strings,
struct ph_stats * const restrict stats)
{
CHECK_OBJ_NOTNULL(stats, PH_STATS_MAGIC);
memset(stats, 0, sizeof(*stats));
if (ph == NULL)
return;
CHECK_OBJ(ph, PH_MAGIC);
CHECK_OBJ_NOTNULL(ph->h1, HASH_MAGIC);
AN(ph->tbl);
AN(ph->collision);
AN(strings);
stats->buckets = ph->h1->mask + 1;
stats->klen = ph->l;
stats->minlen = ph->minlen;
stats->maxlen = ph->maxlen;
for (unsigned i = 0; i <= ph->h1->mask; i++)
if (vbit_test(ph->collision, i)) {
struct hash *h2;
h2 = ph->tbl[i].h2;
CHECK_OBJ_NOTNULL(h2, HASH_MAGIC);
stats->collisions++;
}
}
void
PH_Free(struct ph *ph)
{
......
......@@ -42,6 +42,16 @@
*/
struct ph;
struct ph_stats {
unsigned magic;
#define PH_STATS_MAGIC 0x68b803bb
uint64_t buckets;
uint64_t collisions;
uint64_t klen;
uint64_t minlen;
uint64_t maxlen;
};
/*
* Initialize perfect hashing. Supplies a seed for random number
* generation, which should be obtained from an entropy source. Only
......@@ -76,6 +86,10 @@ unsigned PH_Lookup(const struct ph * const restrict ph,
char * const restrict * const restrict strings,
const char * const restrict subject);
void PH_Stats(const struct ph * const restrict ph,
char * const restrict * const restrict strings,
struct ph_stats * const restrict stats);
/*
* Return a string dump of ph as generated for strings.
*
......
......@@ -115,6 +115,7 @@ main(int argc, char *argv[])
struct timespec before, after, start, finish;
uint64_t ns = 0, iters, matches;
uint32_t seed[4];
struct ph_stats stats = { .magic = PH_STATS_MAGIC };
int opt, do_shuf = 0, do_iters = ITERATIONS;
struct rusage rusage;
......@@ -314,6 +315,17 @@ main(int argc, char *argv[])
printf("... done.\n");
}
printf("\nGetting stats ...\n");
(void)clock_gettime(CLOCK, &before);
PH_Stats(ph, strings, &stats);
(void)clock_gettime(CLOCK, &after);
printf("Stats computed in %lu ns\n", tdiff(&before, &after));
printf("%lu buckets\n", stats.buckets);
printf("%lu collisions\n", stats.collisions);
printf("%lu key vector length\n", stats.klen);
printf("%lu min string length\n", stats.minlen);
printf("%lu max string length\n", stats.maxlen);
if (do_iters == 0)
exit(EXIT_SUCCESS);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment