Author Topic: (Challenge) Closest integer to n  (Read 11811 times)

0 Members and 1 Guest are viewing this topic.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #30 on: February 03, 2010, 05:01:17 AM »
Quote
It was colored dream?
yeah, parenthesis were red, functions - blue, variables - black :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Closest integer to n
« Reply #31 on: February 03, 2010, 08:41:32 AM »
 :-D  Wish all mine were like that.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Closest integer to n
« Reply #32 on: February 03, 2010, 08:54:33 AM »
Very nice VovKa!  8-)

wizman

  • Bull Frog
  • Posts: 290
Re: (Challenge) Closest integer to n
« Reply #33 on: February 03, 2010, 08:58:08 AM »
Good Work vovka, good sleep + good code  :-)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Closest integer to n
« Reply #34 on: February 03, 2010, 11:07:01 AM »
I get this result:

Code: [Select]
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (TEST1-EVGENIY 10 LST)........1217 / 2.91 <fastest>
    (NEAREST41-VOVKA 10 LST)......1232 / 2.87
    (NEAREST133-VOVKA 10 LST).....1280 / 2.77
    (CLOSEST-LEEMAC 10 LST).......1341 / 2.64
    (WIZ-NEAR2 10 LST)............1373 / 2.58
    (WIZ-NEAR 10 LST).............1435 / 2.47
    (CLOSEST2-WHDJR 10 LST).......1451 / 2.44
    (NEAREST-CAB 10 LST)..........1513 / 2.34
    (TEST-EVGENIY 10 LST).........1841 / 1.92
    (NEAREST2-CAB 10 LST).........3541 / 1.00 <slowest>

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #35 on: February 03, 2010, 11:41:00 AM »
Lee, we both will be happier if you benchmark the above with 100 instead of 10 :)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Closest integer to n
« Reply #36 on: February 03, 2010, 11:49:34 AM »
Lee, we both will be happier if you benchmark the above with 100 instead of 10 :)

Nice one  :wink:

Code: [Select]
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST133-VOVKA 100 LST).....1248 / 2.75 <fastest>
    (NEAREST41-VOVKA 100 LST)......1264 / 2.72
    (CLOSEST-LEEMAC 100 LST).......1342 / 2.56
    (WIZ-NEAR2 100 LST)............1342 / 2.56
    (CLOSEST2-WHDJR 100 LST).......1404 / 2.45
    (TEST1-EVGENIY 100 LST)........1451 / 2.37
    (WIZ-NEAR 100 LST).............1466 / 2.34
    (NEAREST-CAB 100 LST)..........1514 / 2.27
    (TEST-EVGENIY 100 LST).........1747 / 1.97
    (NEAREST2-CAB 100 LST).........3433 / 1.00 <slowest>

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #37 on: February 03, 2010, 12:13:46 PM »
i have different results
Code: [Select]
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST41 100 LST)..........1656 / 1.19 <fastest>
    (CLOSEST-LEEMAC 100 LST).....1734 / 1.14
    (NEAREST133 100 LST).........1750 / 1.13
    (WIZ-NEAR2 100 LST)..........1812 / 1.09
    (TEST1 100 LST)..............1969 / 1.00 <slowest>
it looks like autolisp code's speed is cpu dependant

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #38 on: February 03, 2010, 12:24:29 PM »
>VovKa, Lee Mac

Show the arguments with which you have received your results.
I have several options, which in various cases faster your.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Closest integer to n
« Reply #39 on: February 03, 2010, 12:58:20 PM »
>VovKa, Lee Mac

Show the arguments with which you have received your results.
I have several options, which in various cases faster your.

Evgeniy,

I have just used the list as Mark posted in the first post  :-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #40 on: February 03, 2010, 01:10:01 PM »
Ok  :-)

Code: [Select]
(defun test3 (n l / a b)
 (if (vl-position n l)
  n
  (cond ((not (setq l (vl-sort (cons n l) (function <))
                    a (cadr (member n l))
              ) ;_  setq
         ) ;_  not
         (cadr (member n (reverse l)))
        )
        ((not (setq b (cadr (member n (reverse l)))))a)
        ((if(< (abs (- n a)) (abs (- n b))) a b))
  ) ;_  cond
 ) ;_  if
)

Code: [Select]
(setq l '(45 23 63 5 8 56 74 32 96 144 95) n 100)
(BenchMark '((nearest41 n l)(Closest-LEEMAC n l)(test3 n l)))

Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST3 N L)..............1500 / 1.22 <fastest>
    (NEAREST41 N L)..........1531 / 1.19
    (CLOSEST-LEEMAC N L).....1829 / 1 <slowest>

 
_$
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST3 N L)..............1532 / 1.2 <fastest>
    (NEAREST41 N L)..........1547 / 1.19
    (CLOSEST-LEEMAC N L).....1844 / 1 <slowest>

 
_$
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TEST3 N L)..............1515 / 1.21 <fastest>
    (NEAREST41 N L)..........1531 / 1.19
    (CLOSEST-LEEMAC N L).....1829 / 1 <slowest>

 
_$

wizman

  • Bull Frog
  • Posts: 290
Re: (Challenge) Closest integer to n
« Reply #41 on: February 03, 2010, 02:19:07 PM »
Code: [Select]
(defun wiz-near3 (n lst / a d e)
    (cond ((vl-position n lst) n)
          ((setq lst (vl-sort (cons n lst) (function >))
                 lst (cons -1e6 (reverse (cons 1e6 lst)))
                 a   (vl-position n lst)
           )
           (if (< (abs (- (setq d (nth (1+ a) lst)) n))
                  (abs (- (setq e (nth (1- a) lst)) n))
               )
               d
               e
           )

          )
    )
)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Closest integer to n
« Reply #42 on: February 03, 2010, 02:25:16 PM »
You guys want to verify this?

Code: [Select]
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (WIZ-NEAR3 100 LST)...........1201 / 1.12 <fastest>
    (NEAREST41-VOVKA 100 LST).....1216 / 1.10
    (TEST3-EVGENIY 100 LST).......1280 / 1.05
    (CLOSEST-LEEMAC 100 LST)......1342 / 1.00 <slowest>

Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (WIZ-NEAR3 100 LST)...........1185 / 1.15 <fastest>
    (NEAREST41-VOVKA 100 LST).....1232 / 1.10
    (TEST3-EVGENIY 100 LST).......1264 / 1.07
    (CLOSEST-LEEMAC 100 LST)......1357 / 1.00 <slowest>

Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (WIZ-NEAR3 100 LST)...........1170 / 1.15 <fastest>
    (NEAREST41-VOVKA 100 LST).....1201 / 1.12
    (TEST3-EVGENIY 100 LST).......1264 / 1.06
    (CLOSEST-LEEMAC 100 LST)......1341 / 1.00 <slowest>

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #43 on: February 03, 2010, 03:31:04 PM »
upgraded cond to ifs
Code: [Select]
(defun nearest42 (num lst / p a b)
  (if (vl-position num lst)
    num
    (if (zerop (setq lst (vl-sort (cons num lst) (function <))
    p (vl-position num lst) ; faster than Evgeniy's when (> p 2)
                                                    ; faster than wizman's when (not (equal p (length lst) 3))
      )
)
      (cadr lst)
      (if (= (last lst) num)
(nth (1- p) lst)
(if (> (- (setq a (nth (1+ p) lst)) num) (- num (setq b (nth (1- p) lst))))
 b
 a
)
      )
    )
  )
)

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #44 on: February 03, 2010, 03:55:59 PM »
Code: [Select]
(setq lst '(10 20 30 40 50 60 70 80 90 100))
(foreach num lst
  (print (setq num (1+ num)))
  (benchmark '((wiz-near3 num lst) (nearest42 num lst)))
)

Code: [Select]
11 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1485 / 1.03 <fastest>
    (WIZ-NEAR3 NUM LST).....1531 / 1.00 <slowest>

21 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1500 / 1.04 <fastest>
    (WIZ-NEAR3 NUM LST).....1562 / 1.00 <slowest>

31 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1500 / 1.02 <fastest>
    (WIZ-NEAR3 NUM LST).....1531 / 1.00 <slowest>

41 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1516 / 1.03 <fastest>
    (WIZ-NEAR3 NUM LST).....1562 / 1.00 <slowest>

51 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1500 / 1.03 <fastest>
    (WIZ-NEAR3 NUM LST).....1547 / 1.00 <slowest>

61 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1515 / 1.03 <fastest>
    (WIZ-NEAR3 NUM LST).....1563 / 1.00 <slowest>

71 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1531 / 1.02 <fastest>
    (WIZ-NEAR3 NUM LST).....1563 / 1.00 <slowest>

81 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1546 / 1.01 <fastest>
    (WIZ-NEAR3 NUM LST).....1563 / 1.00 <slowest>

91 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (WIZ-NEAR3 NUM LST).....1547 / 1.00 <fastest>
    (NEAREST42 NUM LST).....1547 / 1.00 <slowest>

101 Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (NEAREST42 NUM LST).....1484 / 1.06 <fastest>
    (WIZ-NEAR3 NUM LST).....1578 / 1.00 <slowest>