Command vs. entmake vs. vla-add
If you program with autolisp, you have probably used the command function at some point, probably to construct drawing entities. There is certainly nothing wrong with that. However, if you are working on a large program that constructs a lot of drawing entities, you may have noticed that the command function runs pretty slow.
I put together some tests to compare the (command) function to two other methods of entity creation, (entmake) and (vla-add...). The test constructs 1,999 line entities using various methods.
I originally posted this on 13 DEC 2007, but after receiving a couple of good comments, I decided to edit this original post rather than make a separate entry. Thanks to the comments by Petri and Matt, I added a couple of new tests to the roundup.
Test1 - Uses the (command) function with CMDECHO set to 1
Test2 - Uses the (command) function with CMDECHO set to 0 and NOMUTT set to 1 (this basically turns the command line off...)
Test3 - Uses the (entmake) function.
Test4 - Uses the (vla-add...) function
Test5 - Uses the (vla-add...) function, but moves (vla-get-modelspace (vla-get-ActiveDocument (vlax-get-acad-object))) outside the timer, which make sense because the time to do this task should not be included. Surprisingly, it didn't make much differance at all.
The results are shown below. The times represent the average of 5 consecutive runs.
- Test1 - 11.7 seconds
- Test2 - 5.50 seconds
- Test3 - 0.44 seconds
- Test4 - 0.94 seconds
- Test5 - 0.90 seconds
So (entmake) is still the fastest method, (vla-add...) takes about twice as long as entmake, and the most efficient (command) method about 12X longer on average. Removing the command line echoes from the (command) method speeds this up considerably.
In summary, if you are only drawing few entities, you won't notice any difference by modifying your code. On the other hand, if you code is drawing hundreds of entities, you might consider a change. Keep in mind that these numbers are relative and will vary depending on the system.
The lisp code used is shown below
(defun date2sec ()
(setq s (getvar "DATE"))
(setq seconds (* 86400.0 (- s (fix s))))
)
(defun C:test1 ( / i timer endtimer)
(setvar "cmdecho" 1)
(setq i 1 timer (date2sec))
(while (< i 2000)
(command "._line" (list i i 0.0) (list (+ 2 i)(+ 3 i) 0.0) "")
(setq i (1+ i))
)
(setq endtimer (date2sec))
(alert (rtos (- endtimer timer) 2 8))
)
(defun C:test2 ( / i timer endtimer)
(setvar "cmdecho" 0)
(setvar "nomutt" 1)
(setq i 1 timer (date2sec))
(while (< i 2000)
(command "._line" (list i i 0.0) (list (+ 2 i)(+ 3 i) 0.0) "")
(setq i (1+ i))
)
(setq endtimer (date2sec))
(alert (rtos (- endtimer timer) 2 8))
(setvar "cmdecho" 1)
(setvar "nomutt" 0)
)
(defun C:test3 ( / i timer endtimer)
(setq i 1 timer (date2sec))
(while (< i 2000)
(entmake (list
(cons 0 "LINE")
(cons 10 (list i i 0.0))
(cons 11 (list (+ 2 i)(+ 3 i) 0.0))
)
)
(setq i (1+ i))
)
(setq endtimer (date2sec))
(alert (rtos (- endtimer timer) 2 8))
)
(defun C:test4 ( / i timer endtimer)
(vl-load-com)
(setq i 1 timer (date2sec) ms
(vla-get-modelspace
(vla-get-ActiveDocument
(vlax-get-acad-object))))
(while (< i 2000)
(vla-addline ms
(vlax-3d-point (list i i 0.0))
(vlax-3d-point (list (+ 2 i)(+ 3 i) 0.0))
)
(setq i (1+ i))
)
(setq endtimer (date2sec))
(alert (rtos (- endtimer timer) 2 8))
)
(defun C:test5 ( / i timer endtimer)
(vl-load-com)
(setq ms (vla-get-modelspace
(vla-get-ActiveDocument
(vlax-get-acad-object)
)
)
)
(setq i 1 timer (date2sec))
(while (< i 2000)
(vla-addline ms
(vlax-3d-point (list i i 0.0))
(vlax-3d-point (list (+ 2 i)(+ 3 i) 0.0))
)
(setq i (1+ i))
)
(setq endtimer (date2sec))
(alert (rtos (- endtimer timer) 2 8))
)
2010-03-08, One more update. Read this post over at CADTutor for more details.
http://www.cadtutor.net/forum/showthread.php?p=308808
All content is copyright © CAD Panacea 2008-2010 unless otherwise noted.
All content of CAD Panacea is solely my own personal thoughts and opinions and do not represent my employer or any others.
All comments posted to this blog are the sole responsibility of the person making the comment.
Google, as a third party vendor, uses cookies to serve ads on this site. Google's use of their cookies enables it to serve ads to users based on their visit to your sites and other sites on the Internet. You may opt out of the use of these cookies by visiting the Google ad and content network privacy policy.



