peg.bench 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;;; PEG benchmark suite (minimal right now).
  3. ;; Parses very long equations several times; outputs the average time
  4. ;; it took and the standard deviation of times.
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6. (use-modules (ice-9 pretty-print))
  7. (use-modules (srfi srfi-1))
  8. (use-modules (ice-9 peg))
  9. (use-modules (ice-9 popen))
  10. ;; Generate random equations.
  11. (define (gen-rand-eq len)
  12. (if (= len 0)
  13. (random 1000)
  14. (let ((len (if (even? len) (+ len 1) len)))
  15. (map (lambda (x)
  16. (if (odd? x)
  17. (gen-rand len 'op)
  18. (gen-rand len 'number)))
  19. (iota len)))))
  20. (define (gen-rand len type)
  21. (cond ((eq? type 'number)
  22. (cond
  23. ((= (random 5) 0) (gen-rand-eq (floor (/ len 5))))
  24. (#t (random 1000))))
  25. (#t (list-ref '(+ - * /) (random 4)))))
  26. ;; Generates a random equation string (len is a rough indicator of the
  27. ;; resulting length).
  28. (define (gen-str len)
  29. (with-output-to-string (lambda () (write (gen-rand-eq len)))))
  30. ;; Generates a left-associative parser (see tutorial).
  31. (define (make-left-parser next-func)
  32. (lambda (sum first . rest)
  33. (if (null? rest)
  34. (apply next-func first)
  35. (if (string? (cadr first))
  36. (list (string->symbol (cadr first))
  37. (apply next-func (car first))
  38. (apply next-func (car rest)))
  39. (car
  40. (reduce
  41. (lambda (l r)
  42. (list (list (cadr r) (car r) (apply next-func (car l)))
  43. (string->symbol (cadr l))))
  44. 'ignore
  45. (append
  46. (list (list (apply next-func (caar first))
  47. (string->symbol (cadar first))))
  48. (cdr first)
  49. (list (append rest '("done"))))))))))
  50. ;; Functions for parsing equations (see tutorial).
  51. (define (parse-value value first . rest)
  52. (if (null? rest)
  53. (string->number (cadr first))
  54. (apply parse-sum (car rest))))
  55. (define parse-product (make-left-parser parse-value))
  56. (define parse-sum (make-left-parser parse-product))
  57. (define parse-expr parse-sum)
  58. (define (eq-parse str) (apply parse-expr (peg:tree (match-pattern expr str))))
  59. ;; PEG for parsing equations (see tutorial).
  60. (define-peg-string-patterns
  61. "expr <- sum
  62. sum <-- (product ('+' / '-'))* product
  63. product <-- (value ('*' / '/'))* value
  64. value <-- sp number sp / sp '(' expr ')' sp
  65. number <-- [0-9]+
  66. sp < [ \t\n]*")
  67. ;; gets the time in seconds (with a fractional part)
  68. (define (canon-time)
  69. (let ((pair (gettimeofday)))
  70. (+ (+ (car pair) (* (cdr pair) (expt 10 -6))) 0.0)))
  71. ;; Times how long it takes for FUNC to complete when called on ARGS.
  72. ;; **SIDE EFFECT** Writes the time FUNC took to stdout.
  73. ;; Returns the return value of FUNC.
  74. (define (time-func func . args)
  75. (let ((start (canon-time)))
  76. (let ((res (apply func args)))
  77. (pretty-print `(took ,(- (canon-time) start) seconds))
  78. res)))
  79. ;; Times how long it takes for FUNC to complete when called on ARGS.
  80. ;; Returns the time FUNC took to complete.
  81. (define (time-ret-func func . args)
  82. (let ((start (canon-time)))
  83. (let ((res (apply func args)))
  84. (- (canon-time) start))))
  85. ;; test string (randomly generated)
  86. (define tst1 "(621 - 746 * 945 - 194 * (204 * (965 - 738 + (846)) - 450 / (116 * 293 * 543) + 858 / 693 - (890 * (260) - 855) + 875 - 684 / (749 - (846) + 127) / 670) - 293 - 815 - 628 * 93 - 662 + 561 / 645 + 112 - 71 - (286 - ((324) / 424 + 956) / 190 + ((848) / 132 * 602) + 5 + 765 * 220 - ((801) / 191 - 299) * 708 + 151 * 682) + (943 + 847 - 145 - 816 / 550 - 217 / 9 / 969 * 524 * 447 / 323) * 991 - 283 * 915 / 733 / 478 / (680 + 343 * 186 / 341 * ((571) * 848 - 47) - (492 + 398 * (616)) + 270 - 539 * 34 / 47 / 458) * 417 / 406 / 354 * 678 + 524 + 40 / 282 - 792 * 570 - 305 * 14 + (248 - 678 * 8 - 53 - 215 / 677 - 665 / 216 - 275 - 462 / 502) - 24 - 780 + (967 / (636 / 400 * 823) + 933 - 361 - 620 - 255 / 372 + 394 * 869 / 839 * 727) + (436 + 993 - 668 + 772 - 33 + 64 - 252 * 957 * 320 + 540 / (23 * 74 / (422))) + (516 / (348 * 219 * 986) * 85 * 149 * 957 * 602 / 141 / 80 / 456 / 92 / (443 * 468 * 466)) * 568 / (271 - 42 + 271 + 592 + 71 * (766 + (11) * 946) / 728 / 137 / 111 + 557 / 962) * 179 - 936 / 821 * 101 - 206 / (267 - (11 / 906 * 290) / 722 / 98 - 987 / 989 - 470 * 833 - (720 / 34 - 280) + 638 / 940) - 889 * 84 * 630 + ((214 - 888 + (46)) / 540 + 941 * 724 / 759 * (679 / 527 - 764) * 413 + 831 / 559 - (308 / 796 / 737) / 20))")
  87. ;; appends two equations (adds them together)
  88. (define (eq-append . eqs)
  89. (if (null? eqs)
  90. "0"
  91. (if (null? (cdr eqs))
  92. (car eqs)
  93. (string-append
  94. (car eqs)
  95. " + "
  96. (apply eq-append (cdr eqs))))))
  97. ;; concatenates an equation onto itself n times using eq-append
  98. (define (string-n str n)
  99. (if (<= n 0)
  100. "0"
  101. (if (= n 1)
  102. str
  103. (eq-append str (string-n str (- n 1))))))
  104. ;; standard deviation (no bias-correction)
  105. ;; (also called population standard deviation)
  106. (define (stddev . lst)
  107. (let ((llen (length lst)))
  108. (if (<= llen 0)
  109. 0
  110. (let* ((avg (/ (reduce + 0 lst) llen))
  111. (mapfun (lambda (x) (real-part (expt (- x avg) 2)))))
  112. (sqrt (/ (reduce + 0 (map mapfun lst)) llen))))))
  113. ;; average
  114. (define (avg . lst)
  115. (if (null? lst)
  116. 0
  117. (/ (reduce + 0 lst) (length lst))))
  118. (pretty-print "Parsing equations (see PEG in tutorial). Sample size of 10 for each test.")
  119. (pretty-print
  120. (let ((lst
  121. (map
  122. (lambda (ignore)
  123. (reduce-right
  124. append
  125. 0
  126. (map
  127. (lambda (x)
  128. (let* ((mstr (string-n tst1 x))
  129. (strlen (string-length mstr)))
  130. (let ((func (lambda () (begin (match-pattern expr mstr)
  131. 'done))))
  132. `(((string of length ,strlen first pass)
  133. ,(time-ret-func func))
  134. ((string of length ,strlen second pass)
  135. ,(time-ret-func func))))))
  136. (filter (lambda (x) (= (modulo x 25) 0)) (iota 100)))))
  137. (iota 10))))
  138. (let ((compacted
  139. (reduce-right
  140. (lambda (accum conc)
  141. (map (lambda (l r) (append l (cdr r))) accum conc))
  142. 0
  143. lst)))
  144. (map
  145. (lambda (els)
  146. `(,(car els)
  147. (average time in seconds ,(apply avg (cdr els)))
  148. (standard deviation ,(apply stddev (cdr els)))))
  149. compacted))))
  150. (define (sys-calc str)
  151. (let* ((pipe (open-input-pipe (string-append "echo \"" str "\" | bc -l")))
  152. (str (read pipe)))
  153. (close-pipe pipe)
  154. str))
  155. (define (lisp-calc str)
  156. (+ (eval (eq-parse str) (interaction-environment)) 0.0))
  157. ;; (pretty-print `(,(sys-calc tst1) ,(lisp-calc tst1)))