Alien-Judy
view release on metacpan or search on metacpan
src/judy-1.0.5/src/apps/demo/funhist.c view on Meta::CPAN
// @(#) $Revision: 4.5 $ $Source: /judy/src/apps/demo/funhist.c $
//
// FUNCTION HISTOGRAM; JUDYL DEMO PROGRAM
//
// This program uses JudyL to create a histogram of data generated by the
// function random(3M). Other functions could be substituted in.
//
// The program generates n random numbers, stores them in a JudyL array
// and then prints a histogram. All aspects of the generation and
// histogram are timed so the user can see how long various JudyL functions
// take.
//
//
// This demonstrates:
// JLI (JudyLIns)
// JLF (JudyLFirst)
// JLL (JudyLLast)
// JLN (JudyLNext)
// JLC (JudyLCount)
// JLBC (JudyLByCount)
// how to access a JudyL value.
//
// Notice that using JudyL gives you fast stores and lookups as with hashing
// but without having to define a hash function, initialize the hash table
// or having to predetermine a good hash table size. It also gives you a
// sorted list at the same time.
// Also notice that JudyLCount is much faster than you can sequentially
// count items in an array.
//
#include <stdlib.h> // random()
#include <stdio.h> // printf()
#include <sys/time.h> // gettimeofday()
#include <Judy.h> // JL*() routines
// Default number of iterations (number of random numbers generated)
// This may be overridden on the command line
#define DEFAULT_ITER 1000000
// The number of buckets the histogram is divided into.
#define DEFAULT_HISTO_BUCKETS 32
// Macro for correction english output for plurals
#define PLURAL(count) ((count == 1) ? "" : "s")
// timing routines
struct timeval TBeg, TEnd;
#define STARTTm gettimeofday(&TBeg, NULL)
#define ENDTm gettimeofday(&TEnd, NULL)
#define DeltaUSec \
((double)(TEnd.tv_sec - TBeg.tv_sec) * 1E6 + \
(TEnd.tv_usec - TBeg.tv_usec))
// End of timing routines
int
main(int argc, char **argv)
{
Pvoid_t PJArray = NULL; // main JudyL array
// key is random number, value is repeat count
Pvoid_t PJCount = NULL; // second JudyL array
// key is repeat count (from PJArray)
// value is count of items with the same
// repeat count
Word_t Index; // index in JudyLFirst/Next loop
PWord_t PValue; // pointer to Judy array value
PWord_t PGenCount; // pointer to generation count
Word_t num_vals; // number of randoms to generate
Word_t iter; // for loop iteration
Word_t unique_nums; // number of unique randoms generated
Word_t random_num; // random number
Word_t median; // random number
Word_t tmp1, tmp2; // temporary variable
double random_gen_time; // time to generate random numbers
Word_t histo_incr; // histogram increment
unsigned long long ran_sum = 0; // sum of all generated randoms
// TITLE
printf("\nJudyL demonstration: random(3M) histogram\n");
// SET NUMBER OF RANDOMS TO GENERATE
if (argc != 2)
{
// SET TO DEFAULT_ITER
num_vals = DEFAULT_ITER;
printf("Usage: %s [numvals]\n", argv[0]);
printf(" Since you did not specify a number of values, %ld\n",
num_vals);
printf(" will be used as the number of random numbers to insert\n");
printf(" into the Judy array\n");
}
else
{
// OVERRIDE NUMBER OF RANDOMS TO GENERATE
num_vals = atol(argv[1]);
}
// TIME THE GENERATION OF ALL THE RANDOM NUMBERS. THIS TIME IS LATER
( run in 1.388 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )