Here is what I get from profiling the different dispatch methods in 'unpack'.
First I pack a positive fixnum which is among the first few conditions.

    Cond:
    cpu time: 810 real time: 3367 gc time: 0
    cpu time: 815 real time: 3428 gc time: 0
    cpu time: 815 real time: 3197 gc time: 0
    cpu time: 806 real time: 3045 gc time: 0
    cpu time: 806 real time: 3125 gc time: 0
    ----------------------------------------
    cpu time: 811.5 real time: 3232.4
    
    Case:
    cpu time: 817 real time: 3122 gc time: 0
    cpu time: 835 real time: 3544 gc time: 0
    cpu time: 830 real time: 3337 gc time: 0
    cpu time: 824 real time: 3216 gc time: 0
    cpu time: 840 real time: 3667 gc time: 0
    ----------------------------------------
    cpu time: 829.2 real time: 3377.2
    
    Vector:
    cpu time: 844 real time: 3839 gc time: 0
    cpu time: 826 real time: 3253 gc time: 0
    cpu time: 835 real time: 3194 gc time: 0
    cpu time: 840 real time: 3165 gc time: 0
    cpu time: 838 real time: 3354 gc time: 0
    ----------------------------------------
    cpu time: 836.6 real time: 3361.0

Next I packed a negative fixnum, which is the last condition case:

    Cond:
    cpu time: 953 real time: 3556 gc time: 0
    cpu time: 940 real time: 3035 gc time: 0
    cpu time: 986 real time: 4250 gc time: 2
    cpu time: 946 real time: 3425 gc time: 0
    cpu time: 945 real time: 3233 gc time: 0
    ----------------------------------------
    cpu time: 954.0 real time: 3499.8
    
    Case:
    cpu time: 890 real time: 2820 gc time: 0
    cpu time: 896 real time: 2799 gc time: 0
    cpu time: 904 real time: 2783 gc time: 2
    cpu time: 884 real time: 2751 gc time: 0
    cpu time: 887 real time: 2789 gc time: 0
    ----------------------------------------
    cpu time: 892.2 real time: 2788.4
    
    Vector:
    cpu time: 866 real time: 2704 gc time: 0
    cpu time: 884 real time: 2981 gc time: 0
    cpu time: 880 real time: 2661 gc time: 3
    cpu time: 864 real time: 2647 gc time: 0
    cpu time: 864 real time: 2597 gc time: 0
    ----------------------------------------
    cpu time: 871.6 real time: 2718.0

The average is below the line for each. I tested it by unpacking an array of
2^16 items, all the values were hard-coded so there is no chance of poor macros
messing things up. The dispatch vector is a vector of functions like this:

    (define dispatch
      (vector
        ...
        (lambda (in) (unpack-float 32 in))  ; #xCA
        ...
      ))

The unpacking function looks like this:

    (define (unpack in)
      (define tag (read-byte in))
      (time ((vector-ref dispatch tag) in)))

So in conclusion it looks like the vector is the fastest when it comes to cases
later down the list while the 'cond' is the fastest at first, but slowest at
the end, slower than the vector at the beginning. The case averages somewhere
between the two.

Should I pick 'case' so I get average performance consistently, or should I
pick the vector so I get the best performance in the worst case? I don't think
one can predict which type of data users will most likely want to unpack on
average.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to