(entmake) vs (command) 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 wondered why the routine runs fairly slow.
I put together some tests with the help of many others, to compare the (command) function to two other methods of entity creation, (entmake) and (vla-add…). The test constructs 10,000 line entities using various methods. The results and descriptions are below.

Test 1 – 11.3 Seconds     Uses the (command) function with CMDECHO set to 1
Test 2 – 5.20 Seconds     Uses the (command) function with CMDECHO set to 0 and NOMUTT set to 1 (this basically turns the command line off…)
Test 3 – 0.17 Seconds     Uses the (entmake) function.
Test 4 – 1.10 Seconds     Uses the (vla-add…) function

So (entmake) is the fastest method, and even though (vla-add…) takes about 6X as long as entmake, it is still about 10X faster than using the (command) function when command echoing is enabled. If you disable command line echoing, the (command) function time is cut in half.

In summary, if you are only drawing a few entities, or even a few dozen, you won’t notice any difference using (command) as opposed to the other methods. Just make sure you disable command echoing. On the other hand, if your code is drawing thousands of entities, you might consider converting your code to use (entmake) where possible. Keep in mind that these numbers are relative and will vary depending on the system.

Note: This is a partial repeat post from 2007 on our old site. At that time these test routines were only drawing 2000 entities. In today’s tests we are drawing 10,000 entities in the same time it took 8 years ago.

 

(defun date2sec ()
  (setq s (getvar "DATE"))
  (setq seconds (* 86400.0 (- s (fix s))))
)

(setq iteration 10000)

(defun C:test1 ( / i timer endtimer)
  (setvar "cmdecho" 1)  
  (setq i 0 timer (date2sec))
  (while (< i iteration)
    (command "._line" (list 0.0 i 0.0) (list 1.0 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 0 timer (date2sec))
  (while (< i iteration)
    (command "._line" (list 0.0 i 0.0) (list 1.0 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 0 timer (date2sec))
  (while (< i iteration)
    (entmake (list
        (cons 0 "LINE")
        (cons 10 (list 0.0 i 0.0))
        (cons 11 (list 1.0 i 0.0))
      )
    )    
    (setq i (1+ i))
  )
  (setq endtimer (date2sec))
  (alert (rtos (- endtimer timer) 2 8))
)


(defun C:test4 ( / i timer endtimer ms)
  (vl-load-com)
  (setq ms (vla-get-modelspace
      (vla-get-ActiveDocument
        (vlax-get-acad-object)
      )
    )
  )
  (setq i 0 timer (date2sec))
  (while (< i iteration)
    (vla-addline ms
      (vlax-3d-point (list 0.0 i 0.0))
      (vlax-3d-point (list 1.0 i 0.0))
    )
    (setq i (1+ i))
  )
  (setq endtimer (date2sec))
  (alert (rtos (- endtimer timer) 2 8))
)

1 Comment

  1. Neal Biggio

    Remarkably well-written and informational for a free digital article!
    Thanks for the share!

Comments are closed.