From 9c215008c7f086ee1de6c0b33adfe09f6c46b56e Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 19 Jul 2015 11:48:14 -0400 Subject: [PATCH] Toy routing speed measurement --- Makefile | 2 +- main.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ routingspeed.rkt | 33 ++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 routingspeed.rkt diff --git a/Makefile b/Makefile index 42fdfe9..a0d7846 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: t t: *.c - $(CC) -Wall -O0 -o $@ -g *.c + $(CC) -Wall -Os -o $@ -g *.c clean: rm -f t diff --git a/main.c b/main.c index becfc59..7dee8c7 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "fasthash.h" #include "treetrie.h" @@ -415,6 +417,66 @@ int main(int argc, char *argv[]) { tt_drop(&a, C); } + printf("\n============================================================ C\n"); + { +#define N_PATTERNS 10000 +#define N_MESSAGES 10000000 + tt_node_ptr_t r = TT_EMPTY; + struct timeval start_time, stop_time; + int i; + + gettimeofday(&start_time, NULL); + for (i = 0; i < N_PATTERNS; i++) { + tt_atom_t key = (i << 1) | 1; + tt_node_ptr_t d = tt_grab(&a, tt_dict_singleton(&a, key, TT_EMPTY)); + tt_node_ptr_t p = tt_grab(&a, MAKE_PATH(&a, d, {TT_BOS, key, TT_WILD, TT_EOS})); + tt_replace(&a, &r, tt_trie_union_map(&a, r, p)); + tt_drop(&a, d); + tt_drop(&a, p); + tt_drop(&a, r); /* because tt_trie_union_map returns grab'd */ + } + gettimeofday(&stop_time, NULL); + { + double delta_ms = (stop_time.tv_sec - start_time.tv_sec) * 1000.0 + + (stop_time.tv_usec - start_time.tv_usec) / 1000.0; + printf("Construction time %g ms; that is, %g microsec per pattern, %.12g Hz\n", + delta_ms, + 1000.0 * delta_ms / N_PATTERNS, + N_PATTERNS / (delta_ms / 1000.0)); + } + + printf("r node: %u/%u\n", tt_ptr_idx(r), tt_ptr_tag(r)); + /* tt_arena_flush(&a); */ + /* tt_dump_arena_dot_styled("r", r, &a, TT_STYLE_TEXT_LABELS /\* | TT_STYLE_HIDE_DETAILS *\/); */ + + gettimeofday(&start_time, NULL); + for (i = 0; i < N_MESSAGES; i++) { + tt_atom_t key = ((rand() % N_PATTERNS) << 1) | 1; + tt_node_ptr_t p = r; + p = tt_trie_step(&a, p, TT_BOS); + p = tt_trie_step(&a, p, key); + p = tt_trie_step(&a, p, key); + p = tt_trie_step(&a, p, TT_EOS); + assert(tt_ptr_tag(p) == TT_TAG_OK); + /* printf("\nstepped: key %d, %u/%u\n", key, tt_ptr_idx(p), tt_ptr_tag(p)); */ + /* tt_dump_arena_dot_styled("result", p, &a, TT_STYLE_TEXT_LABELS); */ + } + gettimeofday(&stop_time, NULL); + { + double delta_ms = (stop_time.tv_sec - start_time.tv_sec) * 1000.0 + + (stop_time.tv_usec - start_time.tv_usec) / 1000.0; + printf("Routing time %g ms; that is, %g microsec per routed message, %.12g Hz\n", + delta_ms, + 1000.0 * delta_ms / N_MESSAGES, + N_MESSAGES / (delta_ms / 1000.0)); + } + + tt_drop(&a, r); + printf("\n"); +#undef N_PATTERNS +#undef N_MESSAGES + } + tt_arena_flush(&a); tt_dump_arena_dot_styled("afterAll", TT_NO_PTR, &a, TT_STYLE_TEXT_LABELS); /* expect a_set and b_set here */ diff --git a/routingspeed.rkt b/routingspeed.rkt new file mode 100644 index 0000000..206cb04 --- /dev/null +++ b/routingspeed.rkt @@ -0,0 +1,33 @@ +#lang racket + +(require prospect/route prospect/tset) + +(define N-PATTERNS 10000) +(define N-MESSAGES 1000000) + +(define (pat n) (pattern->matcher (datum-tset n) (list n ?))) + +(define pats + (time + (for/fold [(p (matcher-empty))] + [(n (in-range N-PATTERNS))] + (matcher-union p (pat n))))) + +;; (display (matcher->dot pats)) + +(collect-garbage) +(collect-garbage) +(collect-garbage) + +(define-values (_result cpu-time delta-ms gc-time) + (time-apply + (lambda () + (for [(j (in-range N-MESSAGES))] + (define i (random N-PATTERNS)) + (matcher-match-value pats (list i i) #f))) + '())) + +(printf "Delta ~a ms; that is, ~a microsec per routed message, ~a Hz\n" + delta-ms + (/ (* 1000.0 delta-ms) N-MESSAGES) + (/ N-MESSAGES (/ delta-ms 1000.0)))