Author Topic: Better practices coding  (Read 1947 times)

0 Members and 1 Guest are viewing this topic.

Logan

  • Newt
  • Posts: 41
Better practices coding
« on: August 22, 2017, 05:20:39 PM »
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: [Select]
  1. (defun c:test(/ id)
  2.  
  3.   ;; *positionsPropertiesDB* = Global variable for test
  4.  
  5.   ;; (setq *positionsPropertiesDB*
  6.   ;;      '(
  7.   ;;        (1 (1 . "test1") (2 . "test11") (3 . "test111"))
  8.   ;;        (2 (1 . "test2") (2 . "test22") (3 . "test222"))
  9.   ;;       )
  10.   ;; )
  11.  
  12.   ;; getPositionProperties
  13.   ;; Get list by index
  14.   ;; Call       : (getPositionProperties 1)
  15.   ;; Return     : (1 (1 . "test1") (2 . "test11") (3 . "test111"))
  16.  
  17.   (defun getPositionProperties (positionName)
  18.     (car
  19.       (vl-member-if
  20.         (function
  21.           (lambda (x)
  22.             (= (car x) positionName)
  23.           )
  24.         )
  25.         *positionsPropertiesDB*
  26.       )
  27.     )
  28.   )
  29.  
  30.   ;; addPositionProperties
  31.   ;; Append assoc list by index and refresh global database
  32.   ;; Call       : (addPositionProperties 1 'SYMBOL "VALUE")
  33.   ;; Updates to : (1 (1 . "test1") (2 . "test11") (3 . "test111") (SYMBOL . "VALUE"))
  34.  
  35.   (defun addPositionProperties (positionName
  36.                                 sym
  37.                                 value
  38.                                 /
  39.                                 properties
  40.                                 oldValue)
  41.     (setq properties (getPositionProperties positionName))
  42.     (refreshProperties
  43.       positionName
  44.       (if (setq oldValue (assoc sym (cdr properties)))
  45.         (subst (cons sym value) oldValue properties)
  46.         (append
  47.           properties
  48.           (list (cons sym value))
  49.         )
  50.       )
  51.     )
  52.   )
  53.  
  54.   ;; removePositionProperties
  55.   ;; Remove assoc list by index and refresh global database
  56.   ;; Call       : (removePositionProperties 1 'SYMBOL)
  57.   ;; Updates to : (1 (1 . "test1") (2 . "test11") (3 . "test111"))
  58.  
  59.   (defun removePositionProperties (positionName
  60.                            sym)
  61.     (refreshProperties
  62.       positionName
  63.       (vl-remove-if
  64.         (function
  65.           (lambda (x)
  66.             (and
  67.               (listp x)
  68.               (= (car x) sym)
  69.             )
  70.           )
  71.         )
  72.         (getPositionProperties positionName)
  73.       )
  74.     )
  75.   )
  76.  
  77.   ;; Refresh Properties
  78.   ;; Update global variable
  79.  
  80.   (defun refreshProperties (positionName
  81.                             newProperties)
  82.     (setq *positionsPropertiesDB*
  83.            (mapcar
  84.              (function
  85.                (lambda (properties)
  86.                  (if (= (car properties) positionName)
  87.                    newProperties
  88.                    properties
  89.                  )
  90.                )
  91.              )
  92.              *positionsPropertiesDB*
  93.            )
  94.     )
  95.     (princ)
  96.   )
  97.  
  98.   ;; Delete list by index
  99.   ;; Update global variable
  100.  
  101.   (defun deletePosition (positionName)
  102.     (setq *positionsPropertiesDB*
  103.            (vl-remove-if
  104.              (function
  105.                (lambda (x)
  106.                  (= (car x) positionName)
  107.                )
  108.              )
  109.              *positionsPropertiesDB*
  110.            )
  111.     )
  112.   )
  113.  
  114.   ;; Inserts new indexes
  115.   ;; Update global variable
  116.   (defun addPosition (positionName)
  117.     (setq positionName            (list positionName)
  118.           *positionsPropertiesDB*
  119.                                   (append *positionsPropertiesDB*
  120.                                           (list positionName)
  121.                                   )
  122.     )
  123.   )
  124.  
  125.    ;http://forums.augi.com/showthread.php?121467-Selection-Set-Performance
  126.   (defun BenchTime (start / millisecs)
  127.     (if start
  128.       (setq BenchStart (getvar "Millisecs"))
  129.       (if BenchStart
  130.         (progn
  131.           (princ
  132.             (strcat "\nElapsed: "(rtos (* 0.001 (- (getvar "Millisecs") BenchStart))))
  133.           ) ;_ >princ
  134.           (setq BenchStart nil)
  135.         )
  136.         (princ "\nThere's an error. The bechmark wasn't started yet."
  137.         )
  138.       )
  139.     )
  140.     t
  141.   )
  142.  
  143.   ;---------------------------------------------------------------  
  144.  
  145.   (setq *positionsPropertiesDB* nil
  146.                 *counter* 100) ;Modify as needed
  147.  
  148.   ;Inserts new indexes
  149.   (setq id 0)
  150.   (repeat *counter*
  151.     (setq id (1+ id))
  152.    
  153.     (addPosition id)
  154.   )
  155.  
  156.   ;test 1
  157.   ;Put data
  158.   (princ ">>test 1")
  159.   (setq id 0)
  160.   (repeat *counter*
  161.     (setq id (1+ id))
  162.     (princ (strcat "\n\n-- " (itoa id) " --"))
  163.     (setq BenchStart nil)
  164.     (BenchTime t)
  165.    
  166.     (addPositionProperties id 'PARTNUMBER "000.00.0.00")
  167.     (addPositionProperties id 'GAUGE "18AWG")
  168.     (addPositionProperties id 'DIAMETEREXT 1.6)
  169.     (addPositionProperties id 'COLOR "RED")
  170.     (addPositionProperties id 'MINRADIUS 0.7)
  171.     (addPositionProperties id 'STANDARD "SAE J1128")
  172.     (addPositionProperties id 'FAMILY "FLEX_A")
  173.    
  174.     (BenchTime nil)
  175.   )  
  176.  
  177.   ;test 2
  178.   ;Update data
  179.   (princ ">>test 2")
  180.   (setq id 0)
  181.   (repeat *counter*
  182.     (setq id (1+ id))
  183.     (princ (strcat "\n\n-- " (itoa id) " --"))
  184.     (setq BenchStart nil)
  185.     (BenchTime t)
  186.    
  187.     (addPositionProperties id 'PARTNUMBER "111.11.1.11")
  188.     (addPositionProperties id 'GAUGE "0.75")
  189.     (addPositionProperties id 'DIAMETEREXT 1.3)
  190.     (addPositionProperties id 'COLOR "BLU")
  191.     (addPositionProperties id 'MINRADIUS 0.5)
  192.     (addPositionProperties id 'STANDARD "DIM 72551-6")
  193.     (addPositionProperties id 'FAMILY "FLEX_B")
  194.    
  195.     (BenchTime nil)
  196.   )
  197.  
  198.   ;test 3
  199.   ;remove data
  200.   (princ ">>test 3")
  201.   (setq id 0)
  202.   (repeat *counter*
  203.     (setq id (1+ id))
  204.     (princ (strcat "\n\n-- " (itoa id) " --"))
  205.     (setq BenchStart nil)
  206.     (BenchTime t)
  207.    
  208.     (removePositionProperties id 'PARTNUMBER)
  209.     (removePositionProperties id 'GAUGE)
  210.     (removePositionProperties id 'DIAMETEREXT)
  211.     (removePositionProperties id 'COLOR)
  212.     (removePositionProperties id 'MINRADIUS)
  213.     (removePositionProperties id 'STANDARD)
  214.     (removePositionProperties id 'FAMILY)
  215.    
  216.     (BenchTime nil)
  217.   )
  218.  
  219.   ;test 4
  220.   ;remove index
  221.   (princ ">>test 4")
  222.   (setq id 0)
  223.   (repeat *counter*
  224.     (setq id (1+ id))
  225.    
  226.     (deletePosition id)
  227.   )
  228.  
  229. )
  230.  
« Last Edit: August 24, 2017, 08:09:04 PM by Logan »

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Better practices coding
« Reply #1 on: August 24, 2017, 05:55:57 PM »
My 5 cent
Replace
Code - Auto/Visual Lisp: [Select]
  1. (addPositionProperties id 'PARTNUMBER "000.00.0.00")
  2.     (addPositionProperties id 'GAUGE "18AWG")
  3.     (addPositionProperties id 'DIAMETEREXT 1.6)
  4.     (addPositionProperties id 'COLOR "RED")
  5.     (addPositionProperties id 'MINRADIUS 0.7)
  6.     (addPositionProperties id 'STANDARD "SAE J1128")
  7.     (addPositionProperties id 'FAMILY "FLEX_A")
With
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( a b ) (addPositionProperties id a b))
  2.           '('PARTNUMBER 'GAUGE 'DIAMETEREXT 'COLOR 'MINRADIUS 'STANDARD 'FAMILY )
  3.           '("000.00.0.00" "18AWG" 1.6 "RED" 0.7 "SAE J1128" "FLEX_A")
  4.            )

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Better practices coding
« Reply #2 on: August 24, 2017, 06:18:03 PM »
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.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Logan

  • Newt
  • Posts: 41
Re: Better practices coding
« Reply #3 on: August 25, 2017, 12:41:37 PM »
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

  • Swamp Rat
  • Posts: 1421
Re: Better practices coding
« Reply #4 on: August 26, 2017, 01:07:57 AM »
OK OK i'll return the 5 cent back to you  :-D 8-)

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Better practices coding
« Reply #5 on: August 28, 2017, 10:42:53 AM »
Question... are you using Spaces or Tabs??   :-D
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Better practices coding
« Reply #6 on: August 28, 2017, 05:33:31 PM »
Question... are you using Spaces or Tabs??   :-D

hehe, love Silicon Valley :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Better practices coding
« Reply #7 on: August 28, 2017, 08:03:01 PM »
n00b! Vim has retab.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org