Grafalgo
Library of useful data structures and algorithms
|
00001 #include "stdinc.h" 00002 #include "HashMap.h" 00003 #include "Util.h" 00004 00005 using namespace grafalgo; 00006 00007 int64_t cycCnt(void) 00008 { 00009 uint32_t hi, lo; 00010 uint64_t v64; 00011 00012 /* rdtsc: Loads the time-stamp counter value into %edx:%eax */ 00013 asm volatile ("cpuid; rdtsc; movl %%edx, %0; movl %%eax, %1" /* Read the counter */ 00014 : "=r" (hi), "=r" (lo) /* output */ 00015 : /* input (none) */ 00016 : "%edx", "%eax"); /* Let gcc know whch registers are used */ 00017 00018 v64 = ((uint64_t)hi << 32) + (uint64_t)lo; 00019 00020 return (int64_t)v64; 00021 } 00022 00023 int calibrate() { 00024 int result; 00025 int64_t cyc0 = cycCnt(); uint32_t t0 = Util::getTime(); 00026 usleep(20000); 00027 int64_t cyc1 = cycCnt(); uint32_t t1 = Util::getTime(); 00028 result = (int) ((cyc1-cyc0)/(t1-t0)); 00029 return result; 00030 } 00031 00033 void computeHistogram(int n, int ticksPerUs) { 00034 int repCnt = 10; 00035 HashMap map(n); 00036 uint64_t keys[n+1]; 00037 00038 int *hist = new int[250]; 00039 for (int i = 0; i < 250; i++) hist[i] = 0; 00040 00041 // fill table with pairs having random keys 00042 int miss = 0; 00043 for (int i = 1; i <= n; ) { 00044 keys[i] = random(); keys[i] *= random(); 00045 if (map.put(keys[i],1)) i++; 00046 else miss++; 00047 } 00048 if (miss > 0) 00049 cout << "put failed " << miss << " times during initial " 00050 "insert operations\n"; 00051 int binSize = ticksPerUs/100; 00052 for (int i = 1; i <= n; i++) { 00053 int *samples = new int[repCnt+1]; 00054 for (int j = 1; j <= repCnt; j++) { 00055 samples[j] = randint(1,n); 00056 } 00057 int64_t t0 = cycCnt(); 00058 for (int j = 1; j <= repCnt; j++) { 00059 int k = samples[j]; 00060 map.get(keys[k]); 00061 } 00062 int64_t t1 = cycCnt(); 00063 int diff = (t1-t0)/repCnt; 00064 int k = diff/binSize; 00065 hist[min(249,k)]++; 00066 delete [] samples; 00067 } 00068 for (int i = 248; i >= 0; i--) hist[i] += hist[i+1]; 00069 for (int i = 0; i < 250; i++) { 00070 cout << 5*(2*i+1) << " " << hist[i] << endl; 00071 } 00072 delete [] hist; 00073 } 00074 00075 int main(int argc, char *argv[]) { 00076 int ticksPerUs = calibrate(); 00077 int n = (1 << 20)-1; 00078 if (argc == 2) sscanf(argv[1],"%d",&n); 00079 computeHistogram(n,ticksPerUs); 00080 }