Author Topic: iteration code : short and efficiency challenge  (Read 13715 times)

0 Members and 1 Guest are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 89
iteration code : short and efficiency challenge
« on: July 16, 2015, 10:24:46 PM »
hi

simple code to get an array of points. i tried using macpar,lambda and others....but  coudnt make it work.

is this the only way to achieve the task?

Code: [Select]
(defun getPolarCoors(stp int / lst )
  (setq x 0)
 
   (repeat 4
      (setq lst (cons (polar stp 0.0  (setq x (+ x int)) )lst)) 
     
   )
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #1 on: July 16, 2015, 11:16:17 PM »
Is this the function signature ??

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))
  11.        
  12.  |;
  13.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #2 on: July 17, 2015, 03:45:48 AM »
Is this the function signature ??

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))
  11.        
  12.  |;
  13.  

exactly Kerry!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #3 on: July 17, 2015, 04:04:04 AM »
Good.

I tried about 4 variations and surprisingly polar seemed the fastest.
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (DOIT (LIST 1 2 42) 5).................765 / 1.4011 <slowest>: 0.04669189ms Per iteration
  5.     (INCREMENTXVALUE4 (LIST 1 2 42) 5).....764 / 1.3993
  6.     (DOIT3 (LIST 1 2 42) 5)................764 / 1.3993
  7.     (INCREMENTXVALUE (LIST 1 2 42) 5)......749 / 1.3718
  8.     (GETPOLARCOORS (LIST 1 2 42) 5)........546 / 1.0000 <fastest>: 0.03332520ms Per iteration

I thought this would cream it, but still takes 1.4 times as long as the polar version and ended up being the slowest :)

Code - Auto/Visual Lisp: [Select]
  1. (defun doit (StartPoint Interval / x)
  2.    (setq x 0)
  3.    (mapcar
  4.       (function
  5.          (lambda (pt) (mapcar '+ (list (setq x (+ x Interval)) 0. 0.) pt))
  6.       )
  7.       (list StartPoint StartPoint StartPoint StartPoint)
  8.    )
  9. )
  10.  

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))        
  11.  |;
  12.  
  13. (defun IncrementXvalue (StartPoint Interval / lst pt)
  14.    (setq pt StartPoint )
  15.    (repeat 4
  16.       (setq lst
  17.               (cons (setq pt (mapcar '+ pt (list Interval 0 0)))
  18.                     lst
  19.               )
  20.       )
  21.    )
  22. )
  23.  
« Last Edit: July 17, 2015, 04:23:26 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #4 on: July 17, 2015, 05:05:46 AM »
Cant wait to test and learn it.
What about recrusive function? Maybe its for me to stop being afraid  of them :)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: iteration code : short and efficiency challenge
« Reply #5 on: July 17, 2015, 05:46:03 AM »
recursive:

Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list s) i n))
  8.         ((> n 0) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #6 on: July 17, 2015, 06:30:17 AM »
Good.

I tried about 4 variations and surprisingly polar seemed the fastest.
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (DOIT (LIST 1 2 42) 5).................765 / 1.4011 <slowest>: 0.04669189ms Per iteration
  5.     (INCREMENTXVALUE4 (LIST 1 2 42) 5).....764 / 1.3993
  6.     (DOIT3 (LIST 1 2 42) 5)................764 / 1.3993
  7.     (INCREMENTXVALUE (LIST 1 2 42) 5)......749 / 1.3718
  8.     (GETPOLARCOORS (LIST 1 2 42) 5)........546 / 1.0000 <fastest>: 0.03332520ms Per iteration

I thought this would cream it, but still takes 1.4 times as long as the polar version and ended up being the slowest :)

Code - Auto/Visual Lisp: [Select]
  1. (defun doit (StartPoint Interval / x)
  2.    (setq x 0)
  3.    (mapcar
  4.       (function
  5.          (lambda (pt) (mapcar '+ (list (setq x (+ x Interval)) 0. 0.) pt))
  6.       )
  7.       (list StartPoint StartPoint StartPoint StartPoint)
  8.    )
  9. )
  10.  

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))        
  11.  |;
  12.  
  13. (defun IncrementXvalue (StartPoint Interval / lst pt)
  14.    (setq pt StartPoint )
  15.    (repeat 4
  16.       (setq lst
  17.               (cons (setq pt (mapcar '+ pt (list Interval 0 0)))
  18.                     lst
  19.               )
  20.       )
  21.    )
  22. )
  23.  

Thanx Kerry
ive tested your codes. both generate artificial X so you are limited. and as you said they are slowest.

mapcar is forced to be in this solution...its obviously not its natural place.

as you said polar is the ideal solution, fastest and can retrieve points by angle.

do you agree?

BDW how do you get this time report?


Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #7 on: July 17, 2015, 06:40:48 AM »
recursive:

Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list s) i n))
  8.         ((> n 0) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #8 on: July 17, 2015, 06:44:47 AM »
< .. >

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?

Shay, you'll have to do some study. Google may help with the principles.

added piccy
« Last Edit: July 17, 2015, 07:03:09 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #9 on: July 17, 2015, 06:57:36 AM »
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: iteration code : short and efficiency challenge
« Reply #10 on: July 17, 2015, 07:07:45 AM »
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,

Thank you  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list (mapcar '+ s i)) i n))
  8.         ((> n 1) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #11 on: July 17, 2015, 07:13:48 AM »
Cool !

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (INCREMENTXVALUE (LIST 1 2 42) 5).......749 / 1.4544 <slowest>: 0.04571533ms Per iteration
  5.     (DOIT3 (LIST 1 2 42) 5).................749 / 1.4544
  6.     (INCREMENTXVALUE4 (LIST 1 2 42) 5)......733 / 1.4233
  7.     (F (LIST 1 2 42) (QUOTE (5 2 0)) 4).....733 / 1.4233
  8.     (DOIT (LIST 1 2 42) 5)..................733 / 1.4233
  9.     (GETPOLARCOORS (LIST 1 2 42) 5).........515 / 1.0000 <fastest>: 0.03143311ms Per iteration
  10.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: iteration code : short and efficiency challenge
« Reply #12 on: July 17, 2015, 07:20:12 AM »
 :-D

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #13 on: July 17, 2015, 09:14:44 AM »
If you are only looking for raw performance, I would think the following would be most efficient:

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:f ( p i / x )
  2.     (setq x (car p)
  3.           p (cdr p)
  4.     )
  5.     (list
  6.         (cons (+ x i) p)
  7.         (cons (+ x i i) p)
  8.         (cons (+ x i i i) p)
  9.         (cons (+ x i i i i) p)
  10.     )
  11. )

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):
  2.  
  3.     (LM:F (QUOTE (1 2 42)) 5)..................1107 / 1.87 <fastest>
  4.     (GETPOLARCOORS (QUOTE (1 2 42)) 5).........1373 / 1.51
  5.     (INCREMENTXVALUE (QUOTE (1 2 42)) 5).......1981 / 1.05
  6.     (DOIT (QUOTE (1 2 42)) 5)..................2044 / 1.02
  7.     (F (QUOTE (1 2 42)) (QUOTE (5 0 0)) 4).....2075 / 1 <slowest>

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #14 on: July 17, 2015, 09:41:16 PM »
Hi Lee. THANX. how do you get the time report?