Author Topic: List to String  (Read 10453 times)

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: List to String
« Reply #15 on: June 15, 2013, 01:48:34 PM »
Actually in this case, it seems, the imperative approach out performs the functional:
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s-2 (source token / result)
  2.   (foreach item (reverse source)
  3.     (setq result (cons token (cons item result))))
  4.   (apply 'strcat (cdr result)))
Well, on short lists:
Code: [Select]
_$ (setq test nil)
nil
_$ (length (repeat 10 (setq test (cons "test" test))))
10
_$ (strlen (stringlist->tokenstring test ","))
49
_$ (strlen (l2s test ","))
49
_$ (strlen (l2s-2 test ","))
49
_$
; 3 forms loaded from #<editor "C:/Users/user/Documents/Testing.LSP">
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",")))
Benchmarking ... done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-2 TEST ,)                               16384      1092      1092      1.04
(STRINGLIST->TOKENSTRING TEST ,)             16384      1138      1138      1.00
(L2S TEST ,)                                 16384      1139      1139      1.00
--------------------------------------------------------------------------------
But not on long lists
Code: [Select]
_$ (setq test nil)
nil
_$ (length (repeat 10000 (setq test (cons "test" test))))
10000
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",")))
Benchmarking ... done for 64 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)                64      1077      1077      1.45
(L2S TEST ,)                                    64      1372      1372      1.14
(L2S-2 TEST ,)                                  64      1560      1560      1.00
--------------------------------------------------------------------------------
Must be that reverse in there.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: List to String
« Reply #16 on: June 15, 2013, 02:16:12 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s-2 (source token / result)
  2.   (foreach item (reverse source)
  3.     (setq result (cons token (cons item result))))
  4.   (apply 'strcat (cdr result)))

A minor optimisation:
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s-3 ( source token / result )
  2.     (foreach item (reverse source)
  3.         (setq result (vl-list* token item result))
  4.     )
  5.     (apply 'strcat (cdr result))
  6. )

Another method, for single-character delimiters:
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s-4 ( l s )
  2.     (setq s (ascii s))
  3.     (vl-list->string (cdr (apply 'append (mapcar (function (lambda ( x ) (cons s (vl-string->list x)))) l))))
  4. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: List to String
« Reply #17 on: June 15, 2013, 03:10:58 PM »
Helps a bit, especially the l2s-4 - which is more consistently fast across short/long lists.
Code: [Select]
_$ (setq test nil)
nil
_$ (length (repeat 10 (setq test (cons "test" test))))
10
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ",")))
Benchmarking ..... done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-2 TEST ,)                               16384      1122      1122      1.06
(L2S-3 TEST ,)                               16384      1155      1155      1.03
(L2S-4 TEST ,)                               16384      1155      1155      1.03
(L2S TEST ,)                                 16384      1183      1183      1.00
(STRINGLIST->TOKENSTRING TEST ,)             16384      1184      1184      1.00
--------------------------------------------------------------------------------
_$ (setq test nil)
nil
_$ (length (repeat 10000 (setq test (cons "test" test))))
10000
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ",")))
Benchmarking ..... done for 128 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)               128      1949      1949      1.55
(L2S-4 TEST ,)                                  64      1248      2496      1.21
(L2S-3 TEST ,)                                  64      1295      2590      1.17
(L2S-2 TEST ,)                                  64      1372      2744      1.10
(L2S TEST ,)                                    64      1512      3024      1.00
--------------------------------------------------------------------------------
_$ (setq test nil)
nil
_$ (length (repeat 1000 (setq test (cons "test" test))))
1000
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ",")))
Benchmarking ..... done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)              1024      1622      1622      1.52
(L2S-4 TEST ,)                                1024      1715      1715      1.44
(L2S-3 TEST ,)                                 512      1031      2062      1.19
(L2S-2 TEST ,)                                 512      1061      2122      1.16
(L2S TEST ,)                                   512      1231      2462      1.00
--------------------------------------------------------------------------------
It's only on very long lists where the append apply seems to outperform all. Perhaps I'd go with a conditional function like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun stringlist->tokenstring  (source token / result)
  2.   (cond ((< (length source) 6) (foreach item  (reverse source) (setq result (vl-list* token item result)))
  3.          (apply 'strcat (cdr result)))
  4.         ((< (length source) 50)
  5.          (setq token (ascii token))
  6.          (vl-list->string (cdr (apply 'append (mapcar (function (lambda (x) (cons token (vl-string->list x))))
  7.                                                       source)))))
  8.         (t (apply 'strcat (cdr (apply 'append (mapcar (function (lambda (value) (list token value)))
  9.                                                       source)))))))
From my tests it uses the fastest version in each case. But it seems the cond screws that up on short lists.
Code: [Select]
_$ (progn (setq test nil) (repeat 2 (setq test (cons "test" test))) (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ","))))
Benchmarking ..... done for 32768 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-3 TEST ,)                               32768      1856      1856      1.09
(L2S-2 TEST ,)                               32768      1857      1857      1.09
(STRINGLIST->TOKENSTRING TEST ,)             32768      1950      1950      1.04
(L2S TEST ,)                                 32768      1982      1982      1.02
(L2S-4 TEST ,)                               16384      1014      2028      1.00
--------------------------------------------------------------------------------
_$ (progn (setq test nil) (repeat 5 (setq test (cons "test" test))) (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ","))))
Benchmarking ..... done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-3 TEST ,)                               16384      1014      1014      1.03
(L2S-2 TEST ,)                               16384      1015      1015      1.03
(L2S TEST ,)                                 16384      1045      1045      1.00
(STRINGLIST->TOKENSTRING TEST ,)             16384      1046      1046      1.00
(L2S-4 TEST ,)                               16384      1046      1046      1.00
--------------------------------------------------------------------------------
_$ (progn (setq test nil) (repeat 20 (setq test (cons "test" test))) (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ","))))
Benchmarking ..... done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-4 TEST ,)                               16384      1294      1294      1.19
(STRINGLIST->TOKENSTRING TEST ,)             16384      1357      1357      1.14
(L2S-3 TEST ,)                               16384      1434      1434      1.08
(L2S-2 TEST ,)                               16384      1467      1467      1.05
(L2S TEST ,)                                 16384      1544      1544      1.00
--------------------------------------------------------------------------------
_$ (progn (setq test nil) (repeat 60 (setq test (cons "test" test))) (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ","))))
Benchmarking ..... done for 8192 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(L2S-4 TEST ,)                                8192      1138      1138      1.30
(STRINGLIST->TOKENSTRING TEST ,)              8192      1154      1154      1.28
(L2S-3 TEST ,)                                8192      1280      1280      1.16
(L2S-2 TEST ,)                                8192      1343      1343      1.10
(L2S TEST ,)                                  8192      1482      1482      1.00
--------------------------------------------------------------------------------
_$ (progn (setq test nil) (repeat 1000 (setq test (cons "test" test))) (quickbench '((stringlist->tokenstring test ",") (l2s test ",") (l2s-2 test ",") (l2s-3 test ",") (l2s-4 test ","))))
Benchmarking ..... done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)              1024      1574      1574      1.55
(L2S-4 TEST ,)                                1024      1748      1748      1.39
(L2S-3 TEST ,)                                1024      2010      2010      1.21
(L2S-2 TEST ,)                                 512      1107      2214      1.10
(L2S TEST ,)                                   512      1216      2432      1.00
--------------------------------------------------------------------------------
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.