]> mj.ucw.cz Git - libucw.git/blob - lib/str-test.c
when testing benchmarks of string operations, a user specified alignment
[libucw.git] / lib / str-test.c
1 /*
2  *      Checking the correctness of str_len() and str_hash() and proving, that
3  *      it is faster than the classical version ;-)
4  */
5
6 #include "lib/str_hash.h"
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/time.h>
12
13 /* It will be divided by (10 + strlen()).  */
14 #define TEST_TIME       1000000
15
16 /* The shift of the string according to the alignment.  */
17 static uns alignment = 0;
18
19 static void
20 random_string(char *str, int len)
21 {
22         int i;
23         for (i=0; i<len; i++)
24                 str[i] = random() % 255 + 1;
25         str[len] = 0;
26 }
27
28 static uns
29 elapsed_time(void)
30 {
31         static struct timeval last_tv, tv;
32         uns elapsed;
33         gettimeofday(&tv, NULL);
34         elapsed = (tv.tv_sec - last_tv.tv_sec) * 1000000 + (tv.tv_usec - last_tv.tv_usec);
35         last_tv = tv;
36         return elapsed;
37 }
38
39 int
40 main(int argc, char **argv)
41 {
42         char *strings[] = {
43                 "",
44                 "a",
45                 "aa",
46                 "aaa",
47                 "aaaa",
48                 "aaaaa",
49                 "aaaaaa",
50                 "aaaaaaa",
51                 "aaaaaaaa",
52                 "aaaaaaaaa",
53                 "aaaaaaaaaa",
54                 "\200aaaa",
55                 "\200",
56                 "\200\200",
57                 "\200\200\200",
58                 "\200\200\200\200",
59                 "\200\200\200\200\200",
60                 "kelapS treboR",
61                 "Robert Spalek",
62                 "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu",
63                 "********************************",
64                 "****************************************************************",
65                 NULL
66         };
67         int lengths[] = {
68                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
69                 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
70                 30, 40, 50, 60, 70, 80, 90, 100,
71                 200, 300, 400, 500, 600, 700, 800, 900, 1000,
72                 2000, 4000, 8000, 16000, 32000, 64000,
73                 -1
74         };
75         int i;
76         if (argc > 1)
77                 alignment = atoi(argv[1]);
78         printf("Alignment set to %d\n", alignment);
79         for (i=0; strings[i]; i++)
80                 if (strlen(strings[i]) != str_len(strings[i]))
81                         die("Internal error on string %d", i);
82         printf("%d strings tested OK\n", i);
83         for (i=0; strings[i]; i++)
84                 printf("hash %2d = %08x\n", i, str_hash(strings[i]));
85         for (i=0; lengths[i] >= 0; i++)
86         {
87                 char str[lengths[i] + 1 + alignment];
88                 uns count = TEST_TIME / (lengths[i] + 10);
89                 uns el1 = 0, el2 = 0, elh = 0;
90                 uns tot1 = 0, tot2 = 0, hash = 0;
91                 uns j;
92                 for (j=0; j<count; j++)
93                 {
94                         random_string(str + alignment, lengths[i]);
95                         elapsed_time();
96                         /* Avoid "optimizing" by gcc, since the functions are
97                          * attributed as ((const)).  */
98                         tot1 += strlen(str + alignment);
99                         el1 += elapsed_time();
100                         tot2 += str_len(str + alignment);
101                         el2 += elapsed_time();
102                         hash ^= str_hash(str + alignment);
103                         elh += elapsed_time();
104                 }
105                 if (tot1 != tot2)
106                         die("Internal error during test %d", i);
107                 printf("Test %d: strlen = %d, passes = %d, classical = %d usec, speedup = %.4f\n",
108                         i, lengths[i], count, el1, (el1 + 0.) / el2);
109                 printf("\t\t total hash = %08x, hash time = %d usec\n", hash, elh);
110         }
111 /*
112         printf("test1: %d\n", hash_modify(10000000, 10000000, 99777555));
113         printf("test1: %d, %d\n", i, hash_modify(i, lengths[i-2], 99777333));
114         printf("test1: %d, %d\n", i, hash_modify(lengths[i-2], i, 99777333));
115         printf("test1: %d,%d,%d->%d\n", i, i*3-2, i*i, hash_modify(4587, i*3-2, i*i));
116         printf("test1: %d\n", hash_modify(lengths[5], 345, i));
117 */
118         return 0;
119 }