John,
Love the post! I think this is a great way to help see the differences in how coding affects your efficiency. I do a lot of processing of multiple drawings using ODBX and it helps to know where slow downs take place. Especially when running through 1000+ drawings at a time.
I am typically one to use a While loop over a repeat, just always have. Anyway, I came up with one way using a while loop instead of repeat, and I also made an odd way to do it LOL (it proved to be pretty slow.)
I also did another test using a global variable instead of passing arguments. It does shave off some time, but still not recommended for real use.
I have to ask what machine are you running on? My results were much quicker than yours and the machine I'm running on is pretty slow in my opinion. (Core i5 running AutoCAD R2014) I will have to try this on my faster development system.
Here are the results
(defun revstr3 (mystr / newstr)
(setq newstr "")
(while (/= mystr "")
(setq newstr (strcat newstr (substr mystr (strlen mystr)))
mystr (substr mystr 1 (1- (strlen mystr))))
)
newstr
)
(defun revstr4(mystr / cnt len)
(setq newstr ""
cnt 0
len (strlen mystr)
)
(while (< cnt len)
(setq newstr (strcat newstr (substr mystr (- len cnt) 1))
cnt (1+ cnt)
)
)
newstr
)
(defun revstr1 (old / i new)
(setq new "")
(repeat (setq i (strlen old))
(setq new (strcat new (substr old i 1))
i (1- i)
)
)
new
)
(defun revstr2 (str)
(vl-list->string (reverse (vl-string->list str)))
)
(defun timer (fun n / start stop)
(setq start (getvar "date"))
(repeat n (eval fun))
(setq stop (getvar "date"))
(princ (strcat "Elapsed time for " (itoa n) " iterations: " (rtos (* 86400.0 (- stop start)) 2 2) " secs.\n"))
(eval fun)
)
(setq a "ABCDEFG")
(timer '(revstr1 a) 100000)
(timer '(revstr2 a) 100000)
(timer '(revstr3 a) 100000)
(timer '(revstr4 a) 100000)
Command: (timer '(revstr1 a) 100000)
Elapsed time for 100000 iterations: 1.83 secs.
"GFEDCBA"
Command: (timer '(revstr2 a) 100000)
Elapsed time for 100000 iterations: 1.04 secs.
"GFEDCBA"
Command: (timer '(revstr3 a) 100000)
Elapsed time for 100000 iterations: 2.34 secs.
"GFEDCBA"
Command: (timer '(revstr4 a) 100000)
Elapsed time for 100000 iterations: 1.65 secs.
"GFEDCBA"
Here are the global variable results:
(defun revstr3 ()
(setq newstr ""
mystr a
)
(while (/= mystr "")
(setq newstr (strcat newstr (substr mystr (strlen mystr)))
mystr (substr mystr 1 (1- (strlen mystr))))
)
newstr
)
(defun revstr4()
(setq newstr ""
cnt 0
len (strlen a)
)
(while (< cnt len)
(setq newstr (strcat newstr (substr a (- len cnt) 1))
cnt (1+ cnt)
)
)
newstr
)
(defun revstr1 ()
(setq new "")
(repeat (setq i (strlen a))
(setq new (strcat new (substr a i 1))
i (1- i)
)
)
new
)
(defun revstr2 ()
(vl-list->string (reverse (vl-string->list a)))
)
(defun timer (fun n / start stop)
(setq start (getvar "date"))
(repeat n (eval fun))
(setq stop (getvar "date"))
(princ (strcat "Elapsed time for " (itoa n) " iterations: " (rtos (* 86400.0 (- stop start)) 2 2) " secs.\n"))
(eval fun)
)
(setq a "ABCDEFG")
(timer '(revstr1) 100000)
(timer '(revstr2) 100000)
(timer '(revstr3) 100000)
(timer '(revstr4) 100000)
Command: (timer '(revstr1) 100000)
Elapsed time for 100000 iterations: 1.50 secs.
"GFEDCBA"
Command: (timer '(revstr2) 100000)
Elapsed time for 100000 iterations: 0.77 secs.
"GFEDCBA"
Command: (timer '(revstr3) 100000)
Elapsed time for 100000 iterations: 2.04 secs.
"GFEDCBA"
Command: (timer '(revstr4) 100000)
Elapsed time for 100000 iterations: 1.51 secs.
"GFEDCBA"