Author Topic: Removing some list manipulation from a function decreses performance?  (Read 244 times)

0 Members and 1 Guest are viewing this topic.

V@no

  • Newt
  • Posts: 25
  • AutoCAD 2023
Hello.

I have a function that returns list of fit points of a spline:
Code - Auto/Visual Lisp: [Select]
  1. (DEFUN _getPoints (ent / coord result)
  2.    (WHILE (SETQ coord (ASSOC 11 ent))
  3.       (SETQ result (CONS (CDR coord) result)
  4.             ent    (CDR (MEMBER coord ent))
  5.       )
  6.    )
  7.    (REVERSE result)
  8. )
I use it also to count fit points, which seemed like waste of resources, since I could just count the point within the function instead:
Code - Auto/Visual Lisp: [Select]
  1. (DEFUN _getNumPoints (ent / coord num)
  2.    (setq num 0)
  3.    (WHILE (SETQ coord (ASSOC 11 ent))
  4.       (SETQ ent (CDR (MEMBER coord ent))
  5.             num (1+ num)
  6.       )
  7.    )
  8. )
However, my benchmark shows that _getNumPoints is actually slower, almost twice on spline with 200+ points!
How is this possible? Or my benchmark is incorrect?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ ent _getPoints _isNumPoints test test1 test2)
  2.     (DEFUN _getPoints (ent / coord result)
  3.         (WHILE (SETQ coord (ASSOC 11 ent))
  4.             (SETQ result (CONS (CDR coord) result)
  5.                   ent    (CDR (MEMBER coord ent))
  6.             )
  7.         )
  8.         (REVERSE result)
  9.     )
  10.  
  11.  
  12.     (DEFUN _getNumPoints (ent / coord num)
  13.         (setq num 0)
  14.         (WHILE (SETQ coord (ASSOC 11 ent))
  15.             (SETQ ent (CDR (MEMBER coord ent))
  16.                   num (1+ num)
  17.             )
  18.         )
  19.     )
  20.  
  21.     ; only FIT spline, not CV
  22.     (setq ent (entget (SSNAME (ssget ":S" '((0 . "SPLINE") (-4 . "!=") (74 . 0))) 0)))
  23.     ; start bench
  24.     (setq test1 (car (_vl-times)))
  25.     (repeat 100 (_getNumPoints ent) )
  26.     ;
  27.     (setq test2 (car (_vl-times)))
  28.     (repeat 100 (_getPoints ent) )
  29.     ; result
  30.     (setq test (car (_vl-times)))
  31.     (princ "\n_getNumPoints: ")
  32.     (princ (- test test1))
  33.     (princ "\n_getPoints:    ")
  34.     (princ (- test test2))
  35.     (princ)
  36. )

P.S.
While preparing this question, I've noticed that if I preselect spline before running the test it takes 10 times longer than when spline selected when prompted by the test...huh?
Code: [Select]
Command: TEST <--- pre selected spline
_getNumPoints: 1030
_getPoints:    542
Command:
Command: TEST <-- nothing pre selected
Select objects:
_getNumPoints: 68
_getPoints:    37
« Last Edit: April 20, 2024, 11:34:15 AM by V@no »

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Removing some list manipulation from a function decreses performance?
« Reply #1 on: April 20, 2024, 11:55:20 AM »
(- test test1) includes time spent for both benchmarks
should be (- test2 test1))

V@no

  • Newt
  • Posts: 25
  • AutoCAD 2023
Re: Removing some list manipulation from a function decreses performance?
« Reply #2 on: April 20, 2024, 12:02:12 PM »
/facepalm

Thank you!

Any ideas why it shows different result when object selected prior running the test function, vs object selected during running the function?

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Removing some list manipulation from a function decreses performance?
« Reply #3 on: April 20, 2024, 12:34:41 PM »
Any ideas why it shows different result when object selected prior running the test function, vs object selected during running the function?
probably caused by some internal and thus mysterious reasons :)
add (getstring) after line #22

ribarm

  • Gator
  • Posts: 3288
  • Marko Ribar, architect
Re: Removing some list manipulation from a function decreses performance?
« Reply #4 on: April 20, 2024, 02:00:14 PM »
This part :

Code - Auto/Visual Lisp: [Select]
  1.     (setq test1 (car (_vl-times)))
  2.     (repeat 100 (_getNumPoints ent) )
  3.     (setq test2 (car (_vl-times)))
  4.     (repeat 100 (_getPoints ent) )
  5.     (setq test (car (_vl-times)))
  6.     (princ "\n_getNumPoints: ")
  7.     (princ (- test test1))
  8.     (princ "\n_getPoints:    ")
  9.     (princ (- test test2))
  10.  

Should be :

Code - Auto/Visual Lisp: [Select]
  1.     (setq test1 (car (_vl-times)))
  2.     (repeat 100 (_getNumPoints ent) )
  3.     (setq test2 (car (_vl-times)))
  4.     (repeat 100 (_getPoints ent) )
  5.     (setq test3 (car (_vl-times)))
  6.     (princ "\n_getNumPoints: ")
  7.     (princ (- test2 test1))
  8.     (princ "\n_getPoints:    ")
  9.     (princ (- test3 test2))
  10.  
« Last Edit: April 20, 2024, 02:26:18 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube