Syntax highlighter

2015-02-18

Benchmark of 2 match libraries

I've found a match library for Sagittarius on Github: match-sagittarius. It looks a bit different style of pattern syntax from Andrew Wright's pattern match library or Alex Shinn's one  which is fully compatible with Wright's one so understandable. There are couple of example on the repository so you can find how it looks like. (I think it's pretty cool!)

Now, if there are more than one the same concept library, then you always want to benchmark, don't you? So I made the following script to do:
(import (time) (match) (rename (match match) (match m:match))
        (srfi :1))

(define (count-pair lis)
  (let loop ((i 0) (lis lis))
    (match lis
      (((a . d) rest ...)
       (loop (+ i 1) rest))
      ((x rest ...)
       (loop i rest))
      (() i))))

(define (m:count-pair lis)
  (let loop ((i 0) (lis lis))
     (m:match lis
       (`((,a . ,d) . ,rest)
        (loop (+ i 1) rest))
       (`(,x . ,rest)
        (loop i rest))
       (x i))))

(define lis (list-tabulate 50000 (lambda (i) 
                                   (if (zero? (mod i 5))
                                       (iota (mod i 100))
                                       'x))))

(time (count-pair lis))
(time (m:count-pair lis))

It seems the library doesn't have variable length list match with ... so using pair notation. (This means it can't distinguish pair and list but not sure if it really can't match variable length...)
And I made the library name (match match) for my own sake.
The benchmark result was like this:
$ ~/projects sash -L. -S.sld match-bench.scm                                                                                                                                           

;;  (count-pair lis)
;;  14.316742 real    14.304623 user    0.003922 sys

;;  (m:count-pair lis)
;;  0.009591 real    0.005592 user    0.003994 sys
Boom! Wow! It's pretty fast! For this type of very simple pattern match which I believe most of the cases, this match library can be a good alternative.

No comments:

Post a Comment