Delete char in string

Kay C Lan lan.kc.macmail at gmail.com
Wed Feb 19 20:44:10 EST 2014


On Thu, Feb 20, 2014 at 12:11 AM, Richmond <richmondmathewson at gmail.com>wrote:

>
> Rather than repeat things endlessly here, I would urge interested parties
> to download the stack, mess around with it,
> and crack open the scripts of the 2 buttons.
>
> In line with Geoff's recent post about riding the world of repeat loops
which aren't 'repeat for each' because of the huge benefit of scaling
repeat for each has over other forms, I thought I'd benchmark your code.

My results for 1,000,000 Unicode chars

Your Repeat Until = 237621 ms
Repeat for each = 0 ms

I couldn't believe it either so you'll see I inserted a breakpoint in my
code just to cofirm it was actually recording the time correctly - but
unless you use the Debugger and step through code checking variable values
this may be obvious. So I commented out your repeat and then tested repeat
for each only and it wasn't until 819,200,000 Unicode characters did I get
a 1 ms test time. After that I started crashing LC as I assume I was
hitting some limit.
,
In a New Stack, in a new button - the output is directed to the Message Box:

on mouseUp
   --breakpoint
   --create a random list of 20 ASCII chars between ASCII32 - ASCII 125
   repeat 20 times
      put numToChar(random(94) + 31) after tLIST
   end repeat
   ask "How many characters do you want to process" with 100000 titled
"Enter Digits Only - No Punctuation"
   put it into tNumOfChars
   answer "Do you wish to test against Unicode chars?" with "Yes" or "No"
titled "Test Unicode"
   put it into tUniCode
   if (tUniCode = "Yes") then
      set the useUnicode to true
      put 65535 into tUpperLimit
   else
      put 255 into tUpperLimit
   end if
   repeat tNumOfChars times
      put numToChar(random(tUpperLimit)) after tORIGIN
   end repeat
   --so the 2nd test is identical
   put tORIGIN into tORGIN2
   --TEST 1 TIMING
   put the millisec into tStartTime
   repeat until tORIGIN is empty
      if tLIST contains the first char of tORIGIN then
         put the first char of tORIGIN after tOUTPUT
         delete the first char of tORIGIN
      else
         delete the first char of tORIGIN
      end if
      --put $ORIGIN into fld "fORIGIN2"--not required
   end repeat
   put tOUTPUT into tTest1
   put the millisec into tEndTime
   put tEndTime - tStartTime into tTest1Time

   --TEST 2 TIMING
   put the millisec into tStartTime
   repeat for each char tChar in tORIGIN2
      if (tLIST contains tChar) then
         put tCHAR after tOUTPUT2
      end if
   end repeat
   put tOUTPUT into tTest2
   --breakpoint
   put the millisec into tEndTime
   put tEndTime - tStartTime into tTest2Time

   if (tTest1 = tTest2) then
      put "Repeat Until: " & tTest1Time & cr &  \
      "Repeat for Each: " & tTest2Time into msg
   else
      put "Failed - Outputs not equal" into msg
   end if
   beep
   --breakpoint
end mouseUp



More information about the use-livecode mailing list