• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life is Good.

            Enhance Tech and English
            隨筆 - 65, 文章 - 20, 評(píng)論 - 21, 引用 - 0
            數(shù)據(jù)加載中……

            Boosting Lisp performance - cons vs append and foreach vs while/nth

            From our PD- Nate Holt:

            All,

             More and more of you are finding that you have to deal with this goofy language called AutoLISP. Here are a couple items I pulled together related to some lisp “best practices”. Both apply to optimizing performance when processing large “lists” of data.

            Thanks,

            Nate.

                CONS versus APPEND

            The cons function adds an element to the beginning of a list. The append function can be used to give the equivalent of adding an element to the beginning or end of a list.

            Using the cons function can be MUCH faster than using append to build up a large list.

            We'll run two tests to create a dummy list of 10,000 integer numbers. The first test is using the "cons" function. Appload and type test1 [enter] at the command prompt.

            (defun c:test1 ( / )
              (setq i 1)
              (setq lst nil) ; start out with a blank list
              (repeat 10000
                (setq lst (cons i lst)) ; add next element to beginning of list
                (setq i (1+ i))
              )
              (setq lst (reverse lst)) ; put list back into correct order
              (princ)
            )

            The second test yields the same result but uses the "append" function:

            (defun c:test2 ( / )
              (setq i 1)
              (setq lst nil) ; start out with a blank list
              (repeat 10000
                (setq lst (append lst (list i))) ; append next element on to end of list
                (setq i (1+ i))
              )
              (princ)
            )

            The first test using "cons" builds the 10,000 element list in memory in less than 0.01 seconds (on my T61p).The second test using "append" builds the exact same 10,000 element list in memory but takes a full 3.55 seconds to execute ( ! ). Dealing with large lists, it appears that the "cons" function is many, many times faster.

            ·         FOREACH versus WHILE / NTH

            Let's say you need to cycle through a huge list, one list element at a time. There are two different functions that can cycle through a list, "foreach" and "nth" combined with a "while" loop. When dealing with a very large list, the "foreach" function can be much faster than using a while loop / nth function to index through the list.

            These tests use the 10,000 element list held in memory created be either of the above two tests. This next test uses "foreach" to cycle through the 10,000 element list.

            (defun c:test3 ( / )
              ; use 10,000 element "lst" created by test1 or test2
              (setq find_int (getint "\nFind integer="))
              (setq foundit nil)
              (foreach x lst
                (if (AND (not foundit) (= x find_int))
                  (progn
                    (setq foundit T)
                    (princ " found") 
                ) )   
              )
              (princ)
            )

            This next test does the same thing but uses a "while" loop and the "nth" function to index its way through the 10,000 element list:

            (defun c:test4 ( / )
              ; use 10,000 element "lst" created by test1 or test2
              (setq find_int (getint "\nFind integer="))
              (setq foundit nil)
              (setq ix 0)
              (setq slen (length lst))
              (while (AND (not foundit)(< ix slen))
                (if (= (nth ix lst) find_int) ; look for match
                  (progn ; Found the target element
                    (setq foundit T)
                    (princ " found") 
                ) )
                (setq ix (1+ ix))  
              )
              (princ)
            )

            For the test, looking for integer value 5000 (halfway into the list). The "foreach" function finds and exits in less than 0.01 second. The while loop using the "nth" function finds and exits in 0.07 seconds. Using foreach is significantly faster in processing this large list.

            posted on 2008-11-10 10:49 Mike Song 閱讀(353) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品男人影院| 日韩欧美亚洲综合久久影院Ds| 亚洲中文久久精品无码| 中文无码久久精品| 99久久人人爽亚洲精品美女| 精品久久久久中文字| 久久精品国产精品亚洲精品| 99久久国产热无码精品免费| 久久精品亚洲乱码伦伦中文 | 亚洲国产精品久久久久| 色播久久人人爽人人爽人人片aV| 伊人久久大香线焦AV综合影院| 99久久99久久久精品齐齐| 国内精品久久久久影院老司| jizzjizz国产精品久久| 亚洲欧美一区二区三区久久| 久久国语露脸国产精品电影| 精品久久久久久| 亚洲av成人无码久久精品| 久久久久久久综合日本| 国产精品欧美久久久天天影视 | 蜜桃麻豆WWW久久囤产精品| 久久99国产精品二区不卡| 77777亚洲午夜久久多喷| 国产成人AV综合久久| 国产午夜福利精品久久2021| 亚洲午夜无码久久久久| 中文精品99久久国产| 国产精品欧美久久久久无广告| 国内精品久久久人妻中文字幕| 久久综合鬼色88久久精品综合自在自线噜噜| 久久99精品国产99久久6男男| 伊人久久大香线蕉av不变影院| 久久婷婷五月综合国产尤物app| 久久久青草青青国产亚洲免观| 久久一区二区三区免费| 成人午夜精品久久久久久久小说| 久久夜色精品国产亚洲| 国产成人无码精品久久久久免费| 青青热久久综合网伊人| 久久精品成人免费国产片小草|