Code Red > AutoLISP (Vanilla / Visual)
Better practices coding
Logan:
Hi guys,
is there a better way to do this?
I wrote some functions to handle nested lists.
My question is: if my list gets too large will performance drop when performing update operations?
I Put lots of data in my list using a repeat and I ran some queries and updates.
These functions will not be used the way you are in the example. Sporadically I will update some data.
My interest with this post is to learn good ones better practices for coding.
I hope you can test and give me a feedback.
I used breakpoints between the tests to check how long the action was performed as the size of the list increased.
I went as far as my knowledge allowed me to go.
--- Code - Auto/Visual Lisp: ---(defun c:test(/ id) ;; *positionsPropertiesDB* = Global variable for test ;; (setq *positionsPropertiesDB* ;; '( ;; (1 (1 . "test1") (2 . "test11") (3 . "test111")) ;; (2 (1 . "test2") (2 . "test22") (3 . "test222")) ;; ) ;; ) ;; getPositionProperties ;; Get list by index ;; Call : (getPositionProperties 1) ;; Return : (1 (1 . "test1") (2 . "test11") (3 . "test111")) (defun getPositionProperties (positionName) (car (vl-member-if (function (lambda (x) (= (car x) positionName) ) ) *positionsPropertiesDB* ) ) ) ;; addPositionProperties ;; Append assoc list by index and refresh global database ;; Call : (addPositionProperties 1 'SYMBOL "VALUE") ;; Updates to : (1 (1 . "test1") (2 . "test11") (3 . "test111") (SYMBOL . "VALUE")) (defun addPositionProperties (positionName sym value / properties oldValue) (setq properties (getPositionProperties positionName)) (refreshProperties positionName (if (setq oldValue (assoc sym (cdr properties))) (subst (cons sym value) oldValue properties) (append properties (list (cons sym value)) ) ) ) ) ;; removePositionProperties ;; Remove assoc list by index and refresh global database ;; Call : (removePositionProperties 1 'SYMBOL) ;; Updates to : (1 (1 . "test1") (2 . "test11") (3 . "test111")) (defun removePositionProperties (positionName sym) (refreshProperties positionName (vl-remove-if (function (lambda (x) (and (listp x) (= (car x) sym) ) ) ) (getPositionProperties positionName) ) ) ) ;; Refresh Properties ;; Update global variable (defun refreshProperties (positionName newProperties) (setq *positionsPropertiesDB* (mapcar (function (lambda (properties) (if (= (car properties) positionName) newProperties properties ) ) ) *positionsPropertiesDB* ) ) (princ) ) ;; Delete list by index ;; Update global variable (defun deletePosition (positionName) (setq *positionsPropertiesDB* (vl-remove-if (function (lambda (x) (= (car x) positionName) ) ) *positionsPropertiesDB* ) ) ) ;; Inserts new indexes ;; Update global variable (defun addPosition (positionName) (setq positionName (list positionName) *positionsPropertiesDB* (append *positionsPropertiesDB* (list positionName) ) ) ) ;http://forums.augi.com/showthread.php?121467-Selection-Set-Performance (defun BenchTime (start / millisecs) (if start (setq BenchStart (getvar "Millisecs")) (if BenchStart (progn (princ (strcat "\nElapsed: "(rtos (* 0.001 (- (getvar "Millisecs") BenchStart)))) ) ;_ >princ (setq BenchStart nil) ) (princ "\nThere's an error. The bechmark wasn't started yet." ) ) ) t ) ;--------------------------------------------------------------- (setq *positionsPropertiesDB* nil *counter* 100) ;Modify as needed ;Inserts new indexes (setq id 0) (repeat *counter* (setq id (1+ id)) (addPosition id) ) ;test 1 ;Put data (princ ">>test 1") (setq id 0) (repeat *counter* (setq id (1+ id)) (princ (strcat "\n\n-- " (itoa id) " --")) (setq BenchStart nil) (BenchTime t) (addPositionProperties id 'PARTNUMBER "000.00.0.00") (addPositionProperties id 'GAUGE "18AWG") (addPositionProperties id 'DIAMETEREXT 1.6) (addPositionProperties id 'COLOR "RED") (addPositionProperties id 'MINRADIUS 0.7) (addPositionProperties id 'STANDARD "SAE J1128") (addPositionProperties id 'FAMILY "FLEX_A") (BenchTime nil) ) ;test 2 ;Update data (princ ">>test 2") (setq id 0) (repeat *counter* (setq id (1+ id)) (princ (strcat "\n\n-- " (itoa id) " --")) (setq BenchStart nil) (BenchTime t) (addPositionProperties id 'PARTNUMBER "111.11.1.11") (addPositionProperties id 'GAUGE "0.75") (addPositionProperties id 'DIAMETEREXT 1.3) (addPositionProperties id 'COLOR "BLU") (addPositionProperties id 'MINRADIUS 0.5) (addPositionProperties id 'STANDARD "DIM 72551-6") (addPositionProperties id 'FAMILY "FLEX_B") (BenchTime nil) ) ;test 3 ;remove data (princ ">>test 3") (setq id 0) (repeat *counter* (setq id (1+ id)) (princ (strcat "\n\n-- " (itoa id) " --")) (setq BenchStart nil) (BenchTime t) (removePositionProperties id 'PARTNUMBER) (removePositionProperties id 'GAUGE) (removePositionProperties id 'DIAMETEREXT) (removePositionProperties id 'COLOR) (removePositionProperties id 'MINRADIUS) (removePositionProperties id 'STANDARD) (removePositionProperties id 'FAMILY) (BenchTime nil) ) ;test 4 ;remove index (princ ">>test 4") (setq id 0) (repeat *counter* (setq id (1+ id)) (deletePosition id) ) )
HasanCAD:
My 5 cent
Replace
--- Code - Auto/Visual Lisp: ---(addPositionProperties id 'PARTNUMBER "000.00.0.00") (addPositionProperties id 'GAUGE "18AWG") (addPositionProperties id 'DIAMETEREXT 1.6) (addPositionProperties id 'COLOR "RED") (addPositionProperties id 'MINRADIUS 0.7) (addPositionProperties id 'STANDARD "SAE J1128") (addPositionProperties id 'FAMILY "FLEX_A")With
--- Code - Auto/Visual Lisp: ---(mapcar '(lambda ( a b ) (addPositionProperties id a b)) '('PARTNUMBER 'GAUGE 'DIAMETEREXT 'COLOR 'MINRADIUS 'STANDARD 'FAMILY ) '("000.00.0.00" "18AWG" 1.6 "RED" 0.7 "SAE J1128" "FLEX_A") )
MickD:
Hi HasanCAD,
would using mapcar be faster than the first example? I ask because I find the first example 'clearer' to read and understand, this is important and can save time for when I need to review the code in future.
Then again, I don't write much lisp and your example is probably just as easy to read for an experienced lisper :)
cheers.
Logan:
Thanks for response HasanCAD.
It's a good way to use functions. :-)
My intention is to use this code in the openDCL or objectDCL forms events.
HasanCAD:
OK OK i'll return the 5 cent back to you :-D 8-)
Navigation
[0] Message Index
[#] Next page
Go to full version