Reverse a string

Reverse a string

john.uhden
Mentor Mentor
3,218 Views
7 Replies
Message 1 of 8

Reverse a string

john.uhden
Mentor
Mentor

Not that there is any value in reversing a string, but the following demonstrates the difference between the old ways and the new ways (sometimes).

It's all in fun (that's why I'm here).

 

(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") Command: (timer '(revstr1 a) 100000) Elapsed time for 100000 iterations: 5.99 secs. "GFEDCBA" Command: (timer '(revstr2 a) 100000) Elapsed time for 100000 iterations: 3.17 secs. "GFEDCBA"

I think both will work even on palyndromes.

John F. Uhden

Accepted solutions (1)
3,219 Views
7 Replies
Replies (7)
Message 2 of 8

dennis
Advisor
Advisor

Affectionately referred to as: "Getting your nerd on" or "geeking out"

0 Likes
Message 3 of 8

john.uhden
Mentor
Mentor

I had an electrical engineer friend tell me years ago that, "You can't spell GEEK without a double E."

John F. Uhden

0 Likes
Message 4 of 8

SeeMSixty7
Advisor
Advisor
Accepted solution

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"
0 Likes
Message 5 of 8

john.uhden
Mentor
Mentor

Thanks for all that feedback, Clint.  It's good to see that others like to have fun too.

 

I am using a very sick 2010 Dell I-7 laptop running ACAD 2002 in Win7.  Now that my taxes are done I have to get it admitted to the PC hospital.  I hope it comes out the same door it goes in.

John F. Uhden

0 Likes
Message 6 of 8

SeeMSixty7
Advisor
Advisor

Welcome! Thank you for the post.

 

Good luck on the laptop! Looks like you are due for an AutoCAD update too!

 

Clint

 

 

0 Likes
Message 7 of 8

john.uhden
Mentor
Mentor

Yes, I need an AutoCAD update for sure.  I can get it for a good price, being an ADN member, if I could only afford the dues.  I was unwillingly retired and social security doesn't allow for any extras.  Thank goodness for our reverse mortgage.

John F. Uhden

0 Likes
Message 8 of 8

SeeMSixty7
Advisor
Advisor

Sorry to hear about the "unwilling" retirement. I know a lot of people appreciate your input here it he forums. Maybe nail down some consulting somewhere. Hit Autodesk up to make you a sponsored Forum supporter or something, and give you a copy of AutoCAD.

 

 

0 Likes