]> git.ozlabs.org Git - ccan/blobdiff - ccan/htable/tools/stringspeed.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / htable / tools / stringspeed.c
index 00ad2790cd4cc13e7c58d9ea86f4152d47f7d89d..c6ca10f523aed5f9c7cefc71c55606a744fbf5cd 100644 (file)
@@ -1,9 +1,9 @@
 /* Simple speed tests for a hash of strings. */
 #include <ccan/htable/htable_type.h>
 #include <ccan/htable/htable.c>
-#include <ccan/str_talloc/str_talloc.h>
-#include <ccan/grab_file/grab_file.h>
-#include <ccan/talloc/talloc.h>
+#include <ccan/tal/str/str.h>
+#include <ccan/tal/grab_file/grab_file.h>
+#include <ccan/tal/tal.h>
 #include <ccan/hash/hash.h>
 #include <ccan/time/time.h>
 #include <stdio.h>
@@ -31,65 +31,59 @@ static bool cmp(const char *obj, const char *key)
        return strcmp(obj, key) == 0;
 }
 
-HTABLE_DEFINE_TYPE(char, strkey, hash_str, cmp, str);
+HTABLE_DEFINE_TYPE(char, strkey, hash_str, cmp, htable_str);
 
 /* Nanoseconds per operation */
-static size_t normalize(const struct timeval *start,
-                       const struct timeval *stop,
+static size_t normalize(const struct timeabs *start,
+                       const struct timeabs *stop,
                        unsigned int num)
 {
-       struct timeval diff;
-
-       timersub(stop, start, &diff);
-
-       /* Floating point is more accurate here. */
-       return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
-               / num * 1000;
+       return time_to_nsec(time_divide(time_between(*stop, *start), num));
 }
 
 int main(int argc, char *argv[])
 {
        size_t i, j, num;
-       struct timeval start, stop;
-       struct htable_str *ht;
+       struct timeabs start, stop;
+       struct htable_str ht;
        char **words, **misswords;
 
-       words = strsplit(NULL, grab_file(NULL,
-                                        argv[1] ? argv[1] : "/usr/share/dict/words",
-                                        NULL), "\n");
-       ht = htable_str_new();
-       num = talloc_array_length(words) - 1;
+       words = tal_strsplit(NULL, grab_file(NULL,
+                                            argv[1] ? argv[1] : "/usr/share/dict/words"), "\n",
+                            STR_NO_EMPTY);
+       htable_str_init(&ht);
+       num = tal_count(words) - 1;
+       /* Note that on my system, num is just > 98304, where we double! */
        printf("%zu words\n", num);
 
        /* Append and prepend last char for miss testing. */
-       misswords = talloc_array(words, char *, num);
+       misswords = tal_arr(words, char *, num);
        for (i = 0; i < num; i++) {
                char lastc;
                if (strlen(words[i]))
                        lastc = words[i][strlen(words[i])-1];
                else
                        lastc = 'z';
-               misswords[i] = talloc_asprintf(misswords, "%c%s%c%c",
-                                              lastc, words[i], lastc, lastc);
+               misswords[i] = tal_fmt(misswords, "%c%s%c%c",
+                                      lastc, words[i], lastc, lastc);
        }
 
        printf("#01: Initial insert: ");
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++)
-               htable_str_add(ht, words[i]);
+               htable_str_add(&ht, words[i]);
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
 
        printf("Bytes allocated: %zu\n",
-              sizeof(((struct htable *)ht)->table[0])
-              << ((struct htable *)ht)->bits);
+              sizeof(ht.raw.table[0]) << ht.raw.bits);
 
        printf("#02: Initial lookup (match): ");
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++)
-               if (htable_str_get(ht, words[i]) != words[i])
+               if (htable_str_get(&ht, words[i]) != words[i])
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
@@ -98,7 +92,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++) {
-               if (htable_str_get(ht, misswords[i]))
+               if (htable_str_get(&ht, misswords[i]))
                        abort();
        }
        stop = time_now();
@@ -109,7 +103,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
-               if (htable_str_get(ht, words[j]) != words[j])
+               if (htable_str_get(&ht, words[j]) != words[j])
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
@@ -119,7 +113,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++)
-               if (!htable_str_del(ht, words[i]))
+               if (!htable_str_del(&ht, words[i]))
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
@@ -128,7 +122,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++)
-               htable_str_add(ht, words[i]);
+               htable_str_add(&ht, words[i]);
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
 
@@ -137,7 +131,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i+=2)
-               if (!htable_str_del(ht, words[i]))
+               if (!htable_str_del(&ht, words[i]))
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
@@ -147,7 +141,7 @@ int main(int argc, char *argv[])
 
        start = time_now();
        for (i = 0; i < num; i+=2)
-               htable_str_add(ht, misswords[i]);
+               htable_str_add(&ht, misswords[i]);
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
 
@@ -155,10 +149,10 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 1; i < num; i+=2)
-               if (htable_str_get(ht, words[i]) != words[i])
+               if (htable_str_get(&ht, words[i]) != words[i])
                        abort();
        for (i = 0; i < num; i+=2) {
-               if (htable_str_get(ht, misswords[i]) != misswords[i])
+               if (htable_str_get(&ht, misswords[i]) != misswords[i])
                        abort();
        }
        stop = time_now();
@@ -168,10 +162,10 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i+=2)
-               if (htable_str_get(ht, words[i]))
+               if (htable_str_get(&ht, words[i]))
                        abort();
        for (i = 1; i < num; i+=2) {
-               if (htable_str_get(ht, misswords[i]))
+               if (htable_str_get(&ht, misswords[i]))
                        abort();
        }
        stop = time_now();
@@ -182,9 +176,9 @@ int main(int argc, char *argv[])
        printf("#11: Churn 1: ");
        start = time_now();
        for (j = 0; j < num; j+=2) {
-               if (!htable_str_del(ht, misswords[j]))
+               if (!htable_str_del(&ht, misswords[j]))
                        abort();
-               if (!htable_str_add(ht, words[j]))
+               if (!htable_str_add(&ht, words[j]))
                        abort();
        }
        stop = time_now();
@@ -193,9 +187,9 @@ int main(int argc, char *argv[])
        printf("#12: Churn 2: ");
        start = time_now();
        for (j = 1; j < num; j+=2) {
-               if (!htable_str_del(ht, words[j]))
+               if (!htable_str_del(&ht, words[j]))
                        abort();
-               if (!htable_str_add(ht, misswords[j]))
+               if (!htable_str_add(&ht, misswords[j]))
                        abort();
        }
        stop = time_now();
@@ -204,9 +198,9 @@ int main(int argc, char *argv[])
        printf("#13: Churn 3: ");
        start = time_now();
        for (j = 1; j < num; j+=2) {
-               if (!htable_str_del(ht, misswords[j]))
+               if (!htable_str_del(&ht, misswords[j]))
                        abort();
-               if (!htable_str_add(ht, words[j]))
+               if (!htable_str_add(&ht, words[j]))
                        abort();
        }
        stop = time_now();
@@ -217,7 +211,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++)
-               if (htable_str_get(ht, words[i]) != words[i])
+               if (htable_str_get(&ht, words[i]) != words[i])
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));
@@ -226,7 +220,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0; i < num; i++) {
-               if (htable_str_get(ht, misswords[i]))
+               if (htable_str_get(&ht, misswords[i]))
                        abort();
        }
        stop = time_now();
@@ -237,7 +231,7 @@ int main(int argc, char *argv[])
        fflush(stdout);
        start = time_now();
        for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
-               if (htable_str_get(ht, words[j]) != words[j])
+               if (htable_str_get(&ht, words[j]) != words[j])
                        abort();
        stop = time_now();
        printf(" %zu ns\n", normalize(&start, &stop, num));