Author Topic: Compare speed of iterators / Speed Test  (Read 1428 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Compare speed of iterators / Speed Test
« on: May 10, 2017, 05:34:26 PM »
Hi guys,
just out of curiosity I attempted this:

Code - Auto/Visual Lisp: [Select]
  1. (defun CompareSpeed ( fL / L f time timeL )
  2.   (setq L (mapcar 'chr (vl-string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
  3.   (setq f (lambda ( x ) (strcase x t)))
  4.   (foreach x fL
  5.     (repeat 5
  6.       (setq time (getvar 'MilliSecs))
  7.       (repeat 10000 (eval (cadr x)) )
  8.       (setq timeL (cons (* (- (getvar 'MilliSecs) time) 1e-3) timeL))
  9.     ); repeat
  10.     (print (strcat (vl-prin1-to-string (car x)) " = " (vl-prin1-to-string ((lambda (x) (/ (apply '+ x) (length x))) timeL))))
  11.     (princ)
  12.   ); foreach
  13. ); defun CompareSpeed

The test performed:
Code - Auto/Visual Lisp: [Select]
  1. (CompareSpeed ; Compare the speed of different iterators
  2.   '(
  3.     (repeat (progn (setq i -1) (repeat (length L) (f (nth (setq i (1+ i)) L)))))
  4.     (foreach (foreach x L (f x)) )
  5.     (whileNoCheck (lambda (L) (setq L (cons nil L)) (while (setq L (cdr L)) (f L)) ) )
  6.     (whileWithCheck (lambda (L) (setq L (cons nil L)) (while (and (setq L (cdr L)) (/= "M" (car L))) (f L)) ) )
  7.     (RecursionNoCheck (progn (defun rec ( f L ) (cond  ( (not L) L) ( (cons (f (car L)) (rec f (cdr L))) ) ) ) (rec f L) ) )
  8.     (RecursionWithCheck (progn (defun rec ( f L ) (cond  ( (not L) L) ( (= "M" (car L)) ) ( (cons (f (car L)) (rec f (cdr L))) ) ) ) (rec f L) ) )
  9.     (mapcara (mapcar 'f L) ) ; using apostrophe
  10.     (mapcarf (mapcar (function f) L) ) ; using (function) function
  11.     (vl-some (vl-some '(lambda (x) (= "M" x)) L))
  12.     (vl-every (vl-every '(lambda (x) (not (= "M" x))) L))
  13.   ); list
  14. ); speedtest
  15.  

And these are the results I got:
; "REPEAT = 0.7468"
; "FOREACH = 0.6687"
; "WHILENOCHECK = 0.688533"
; "WHILEWITHCHECK = 0.7"
; "RECURSIONNOCHECK = 0.70624"
; "RECURSIONWITHCHECK = 0.708833"
; "MAPCARA = 0.691514"
; "MAPCARF = 0.678925"
; "VL-SOME = 0.725356"
; "VL-EVERY = 0.76282"

Its my first time attempting speed test, so my questions are:
  • Does the above seem correct?
  • How would you perform speed test for a coupe of functions?


I thought that the most handy and readable way would be to have a subfunction like the above one (and maybe even vl-sort the results at the end).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Compare speed of iterators / Speed Test
« Reply #1 on: May 10, 2017, 06:36:02 PM »
  • How would you perform speed test for a coupe of functions?
the most common is MP's benchmark.lsp
http://www.theswamp.org/index.php?topic=3952.0;all

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Compare speed of iterators / Speed Test
« Reply #2 on: May 11, 2017, 04:40:22 AM »
Thanks Vovka!
I've forgot about the "benchmark" keyword.

Posted as text file so it's accessible for those souls with over protective filtering ...

http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt?nossi=1

:lol:

EDIT: Sample results I got with MP's benchmark:
Code: [Select]
_$ (BenchMark ; Compare the speed of different iterators
  '(
    (progn (setq i -1) (repeat (length L) (f (nth (setq i (1+ i)) L))))
    (foreach x L (f x))
    (lambda (L) (setq L (cons nil L)) (while (setq L (cdr L)) (f L)) )
    (lambda (L) (setq L (cons nil L)) (while (and (setq L (cdr L)) (/= "M" (car L))) (f L)) )
    (progn (defun rec ( f L ) (cond  ( (not L) L) ( (cons (f (car L)) (rec f (cdr L))) ) ) ) (rec f L) )
    (progn (defun rec ( f L ) (cond  ( (not L) L) ( (= "M" (car L)) ) ( (cons (f (car L)) (rec f (cdr L))) ) ) ) (rec f L) )
    (mapcar 'f L)  ; using apostrophe
    (mapcar (function f) L)  ; using (function) function
    (vl-some '(lambda (x) (= "M" x)) L)
    (vl-every '(lambda (x) (not (= "M" x))) L)
  ); list
); BenchMark
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (MAPCAR (FUNCTION F) L)......................1188 / 2.03 <fastest>
    (MAPCAR (QUOTE F) L).........................1204 / 2
    (LAMBDA (L) (SETQ L (CONS nil L)) (W...).....1234 / 1.95
    (LAMBDA (L) (SETQ L (CONS nil L)) (W...).....1531 / 1.57
    (VL-EVERY (QUOTE (LAMBDA (X) (NOT (=...).....1734 / 1.39
    (FOREACH X L (F X))..........................1796 / 1.34
    (VL-SOME (QUOTE (LAMBDA (X) (= "M" X...).....1875 / 1.28
    (PROGN (SETQ I -1) (REPEAT (LENGTH L...).....2266 / 1.06
    (PROGN (DEFUN REC (F L) (COND ((NOT ...).....2375 / 1.01
    (PROGN (DEFUN REC (F L) (COND ((NOT ...).....2406 / 1 <slowest>
_$
« Last Edit: May 11, 2017, 05:12:34 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg