2 * Checking the correctness of str_len() and hash_*() and proving, that
3 * it is faster than the classical version ;-)
6 #include <ucw/hashfunc.h>
13 /* It will be divided by (10 + strlen()). */
14 #define TEST_TIME 1000000
16 /* The shift of the string according to the alignment. */
17 static uint alignment = 0;
20 random_string(byte *str, int len)
24 str[i] = random() % 255 + 1;
31 static struct timeval last_tv, tv;
33 gettimeofday(&tv, NULL);
34 elapsed = (tv.tv_sec - last_tv.tv_sec) * 1000000 + (tv.tv_usec - last_tv.tv_usec);
40 main(int argc, char **argv)
60 "\200\200\200\200\200",
63 "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu",
64 "********************************",
65 "****************************************************************",
69 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
70 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
71 30, 40, 50, 60, 70, 80, 90, 100,
72 200, 300, 400, 500, 600, 700, 800, 900, 1000,
73 2000, 4000, 8000, 16000, 32000, 64000,
78 alignment = atoi(argv[1]);
79 printf("Alignment set to %d\n", alignment);
80 for (i=0; strings[i]; i++)
81 if (strlen(strings[i]) != str_len(strings[i]))
82 die("Internal str_len() error on string %d", i);
83 printf("%d strings tested OK\n", i);
84 for (i=0; strings[i]; i++)
87 h1 = hash_string(strings[i]);
88 h2 = hash_string_nocase(strings[i]);
89 if (h1 != hash_block(strings[i], str_len(strings[i])))
90 die("Internal hash_string() error on string %d", i);
91 printf("hash %2d = %08x %08x", i, h1, h2);
93 printf(" upper case?");
96 for (i=0; lengths[i] >= 0; i++)
98 byte str[lengths[i] + 1 + alignment];
99 uint count = TEST_TIME / (lengths[i] + 10);
100 uint el1 = 0, el2 = 0, elh = 0, elhn = 0;
101 uint tot1 = 0, tot2 = 0, hash = 0, hashn = 0;
103 for (j=0; j<count; j++)
105 random_string(str + alignment, lengths[i]);
107 /* Avoid "optimizing" by gcc, since the functions are
108 * attributed PURE. */
109 tot1 += strlen(str + alignment);
110 el1 += elapsed_time();
111 tot2 += str_len(str + alignment);
112 el2 += elapsed_time();
113 hash ^= hash_string(str + alignment);
114 elh += elapsed_time();
115 hashn ^= hash_string_nocase(str + alignment);
116 elhn += elapsed_time();
119 die("Internal error during test %d", i);
120 printf("Test %d: strlen = %d, passes = %d, classical = %d usec, speedup = %.4f\n",
121 i, lengths[i], count, el1, (el1 + 0.) / el2);
122 printf("\t\t total hash = %08x/%08x, hash time = %d/%d usec\n", hash, hashn, elh, elhn);
125 printf("test1: %d\n", hash_modify(10000000, 10000000, 99777555));
126 printf("test1: %d, %d\n", i, hash_modify(i, lengths[i-2], 99777333));
127 printf("test1: %d, %d\n", i, hash_modify(lengths[i-2], i, 99777333));
128 printf("test1: %d,%d,%d->%d\n", i, i*3-2, i*i, hash_modify(4587, i*3-2, i*i));
129 printf("test1: %d\n", hash_modify(lengths[5], 345, i));