Author Topic: Split String With String  (Read 6054 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Split String With String
« Reply #15 on: June 13, 2017, 07:00:37 PM »
Talking about speed.

A simple F# implementation:
Code - F#: [Select]
  1. open System.Text.RegularExpressions
  2.  
  3. let foo pat str =
  4.     let rec loop acc = function
  5.         | [] | [""] -> List.rev acc
  6.         | [s]       -> List.rev (s :: acc)
  7.         | "" :: l   -> loop (pat :: acc) l
  8.         | s :: l    -> loop (pat :: s :: acc) l
  9.     Regex.Split(str, Regex.Escape(pat))
  10.     |> Array.toList
  11.     |> loop []

A little bench for 65536 iterations give results about 10 times faster than AutoLISP
Code - F#: [Select]
  1. let bench () =
  2.     let sw = System.Diagnostics.Stopwatch()
  3.     sw.Start()
  4.     for i in 0..65535 do
  5.         foo "$#*" "$#*ABC$#*DEF$#*GHIJKL$#*MNOP$#*" |> ignore
  6.     sw.Stop()
  7.     printfn "Elapsed milliseconds for 65536 iterations: %d" sw.ElapsedMilliseconds

Code: [Select]
> bench ();;
Elapsed milliseconds for 65536 iterations: 123
val it : unit = ()


EDIT: I was tired last night, Regular Expression can do it quite directly:
Code - F#: [Select]
  1. let foo pat str =
  2.     Regex.Split(str, "(" + Regex.Escape(pat) + ")")
  3.     |> Array.filter(fun s -> s <> "")
« Last Edit: June 14, 2017, 06:14:36 AM by gile »
Speaking English as a French Frog

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Split String With String
« Reply #16 on: June 13, 2017, 11:29:55 PM »
Ok... take look at the arguments:
Code: [Select]
(_foo  Keys String)
(foo   Keys String)
(split String Keys)
(ALE   String Keys)
I see that now .. guess I should not assume that initial form would follow in testing.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Split String With String
« Reply #17 on: June 14, 2017, 08:45:33 AM »
About recursion, with long string we can see more differences:
Code: [Select]
(progn
(setq Keys "$#*" String "$#*ABC$#*DEF$#*GHIJKL$#*MNOP$#*")
(repeat 8 (setq String (strcat String Keys String))) ; la ricorsione arriva fino a 11
(print (strlen String))
(princ)
)
Comando: (strlen String)
8701

Elapsed milliseconds / relative speed for 1024 iteration(s):
    (ALE STRING KEYS).....1531 / 4.34 <fastest>
    (ALE STRING KEYS).....1531 / 4.34
    (FOO KEYS STRING).....6547 / 1.01
    (FOO KEYS STRING).....6641 / 1 <slowest>


VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Split String With String
« Reply #18 on: June 14, 2017, 09:30:45 AM »
Interesting enough I fixed the benchmark and Lee's split is still the fastest?
you've switched the arguments in Lee's function only once, but should've done it thrice :)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Split String With String
« Reply #19 on: June 14, 2017, 09:34:40 AM »
ronjonp,

It looks like you have a wrong version of the 'split' routine.
With the version you psted
Code - Auto/Visual Lisp: [Select]
  1. _$ (split "$#*" str)
returns ("" "$#*" "$#*")

Before hunting milliseconds with benchmarks, we should make sure that the routines work as expected.
Speaking English as a French Frog

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Split String With String
« Reply #20 on: June 14, 2017, 10:18:40 AM »
Interesting enough I fixed the benchmark and Lee's split is still the fastest?
you've switched the arguments in Lee's function only once, but should've done it thrice :)
DOH!  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Split String With String
« Reply #21 on: June 15, 2017, 10:01:36 AM »
Tested on Bricscad V17, recursion is very slow:
Code: [Select]
(progn
(setq Keys "$#*" String "$#*$#*ABC$#*DEF$#*$#*GHIJKL$#*MNOP$#*$#*")
(repeat 6 (setq String (strcat String Keys String)))
(print (strlen String)) (princ " strlen\n")
(princ)
)
2749  strlen
Elapsed milliseconds / relative speed for 16384 iteration(s):
    (ALE STRING KEYS)......2938 / 11.89 <fastest>
    (FOO KEYS STRING).....34922 / 1 <slowest>

...another bug with adjacent key, similar my first version:
Code: [Select]
(defun ALE (s d / p m l o r)
  ; 20170614 - version 1.03 for adjacent key (d)
  (cond
    ( (setq
        l (strlen d)          p 0
        m (vl-string-search d s 0)
      )
      (while m
        (if (zerop (- m p))
          (setq o (cons d o))
          (setq o (cons d (cons (substr s (1+ p) (- m p)) o)))
        )
        (setq
          p (+ m l)
          m (vl-string-search d s p)
        )
      )
      (if (= "" (setq r (substr s (1+ p))))
        (reverse o)
        (reverse (cons r o))
      )
    )
    ( (list s) )
  )
)