Sweety Posted September 18, 2010 Posted September 18, 2010 Hello. Is there any difference between these two gained start points of entity ? (setq e (car (entsel "\nSelect Line:"))) This one .. (setq pt (cdr (assoc 10 (entget e)))) And this one .. (setq pt1 (cons (cdr (assoc 10 (entget e))) pt1)) Although the two return values are the same . Many Thanks. Quote
Lee Mac Posted September 18, 2010 Posted September 18, 2010 The second has redundant code if not used within some kind of loop. For a single evaluation, both are identical methods - the second just has irrelevant code, making it more inefficient. I would suggest you investigate the 'cons' function - read up on it in the ACAD Dev Help file. Examples of use of 'cons': Creating a dotted pair, usings 'cons' on two atoms: (cons a b) ==> (a . b) Note that a dotted pair can only be created from two atoms: (cons '(a b) '(c d)) ==> ((a b) c d) Adding an item to a list: (cons a '(b c d e)) ==> (a b c d e) (cons '(a b) '(c d e f)) ==> ((a b) c d e f) Lee Quote
Sweety Posted September 18, 2010 Author Posted September 18, 2010 OK. very nice. I guess it's better to deal or to use the first code in my example to get a coordinate point. But for the second gained point which is related to 'cons' function, it's got to be used for recombing coordinates or dotted pairs as a better and faster way of construction, which is somehow you related to. Great thanks to you Lee for your kind views. Quote
Lee Mac Posted September 18, 2010 Posted September 18, 2010 'cons' is commonly used as a fast way to build lists when in a loop; there is the downside that the list will be reversed, but it is faster than using 'append'. e.g. (foreach x '(1 2 3 4 5) (setq lst (cons x lst)) ) ==> lst : (5 4 3 2 1) Alternative: (foreach x '(1 2 3 4 5) (setq lst (append lst (list x))) ) ==> lst : (1 2 3 4 5) Quote
Sweety Posted September 18, 2010 Author Posted September 18, 2010 With your first example, I have seen it in defun 'massoc' but I do not remeber it that well, which is used with 'reverse' function to rebuild the list in sequence once again. So for the second one in your examples, it would shorten our job in use of 'reverse' function. A very powerful examples. Thank you so much. Quote
Kerry Brown Posted September 18, 2010 Posted September 18, 2010 You may find that using cons then reverse is faster than using append. .. at least thats my recollection. but the time difference would not concern most casual code applications. (defun _t1 () (foreach x '(1 2 3 4 5) (setq lst (cons x lst))) (setq lst (reverse lst)) ) (defun _T2 () (foreach x '(1 2 3 4 5) (setq lst (append lst (list x))))) (benchmark '( (_t1) (_t2))) Benchmarking [M.P. 2005 ] ............ Elapsed milliseconds for 512 iteration(s)/ relative Timing : (_T2).....11045 / 6.6778 (_T1)......1654 / 1.0000 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.