delete end chars

Kay C Lan lan.kc.macmail at gmail.com
Sun Aug 24 23:09:41 EDT 2014


Not bringing much to the table other than to say that if you are
looking into large amounts of data, and need to do this millions of
time, and therefore speed is of interest, Peter's grep solution is
ever so slightly faster than Colin's. There isn't much in it and both
scale well. If you'd like to test, create a new stack, one button  and
in the btn copy the below script which is one mouseUp handler and one
Function fRandomInRange.

The script will ask you how long you want your test string to be and
how many times to you want to repeat the search. The default values of
a string 100 chars long searched a million times (repeated 4 times)
takes just over a second so don't be worried about starting with the
default values. The search is repeated 4 times, 1st it does Colin's
search, then it does Peter's search and just in case there is some
kind of advantage/disadvantage regarding being first, I do Peter's
search again and end off with Colin's.

When run the message box should end up with something like this:

For a random string of 100 chars long.
Searched 1000000 times.
Colin's 1st run: 362 ms
Peter's 1st run: 349 ms
Output Identical
Peter's 2nd run: 351 ms
Output Identical
Colin's 2nd run: 356 ms
Output Identical

BE MINDFUL OF LINE WRAPS

on mouseUp
   ask "How many characters do you want in your random string" with
100 titled "Enter a Number"
   put it - 3 into tAnswer
   put "For a random string of " & tAnswer + 3 & " chars long." & cr into msg
   repeat tAnswer times
      --generates a number between 32 & 90 and turns it into an ASCII char
      put numToChar(fRandomInRange(32,90)) after tData
   end repeat
   --so we know that the data contains the string to be found
   put "ABC" after tData
   put "ABC" into tFind
   ask "How many times do you want to repeat the search" with 1000000
titled "Enter a Number"
   put it into tAnswer
   put "Searched " & tAnswer & " times." & cr after msg
   put the millisec into tStart
   --Colin's 1st
   repeat tAnswer times
      put offset(tFind,tSearch) into tOffset
      if (tOffset = the number of chars in tSearch - the number of
chars in tFine + 1) then
         put char 1 to tOffset - 1 of tSearch into tFound
         put tFound into tColin1st
      end if
   end repeat
   put the millisec into tEnd
   put "Colin's 1st run: " & tEnd - tStart & " ms" & cr after msg
   put the millisec into tStart
   --Peter's 1st run
   repeat tAnswer times
      if (matchText(tSearch,"(.*)" & tFind & "$",tFound)) then
         put tFound into tPeter1st
      end if
   end repeat
   put the millisec into tEnd
   put "Peter's 1st run: " & tEnd - tStart & " ms" & cr after msg
   if (tColin1st = tPeter1st) then
      put "Output Identical" & cr after msg
   else
      put "ERROR!" & cr after msg
   end if
   put the millisec into tStart
   --Peter's Again
   repeat tAnswer times
      if (matchText(tSearch,"(.*)" & tFind & "$",tFound)) then
         put tFound into tPeter2nd
      end if
   end repeat
   put the millisec into tEnd
   put "Peter's 2nd run: " & tEnd - tStart & " ms" & cr after msg
   if (tPeter2nd = tPeter1st) then
      put "Output Identical" & cr after msg
   else
      put "ERROR!" & cr after msg
   end if
   put the millisec into tStart
   --Colin's 2nd run
   repeat tAnswer times
      put offset(tFind,tSearch) into tOffset
      if (tOffset = the number of chars in tSearch - the number of
chars in tFine + 1) then
         put char 1 to tOffset - 1 of tSearch into tFound
         put tFound into tColin2nd
      end if
   end repeat
   put the millisec into tEnd
   put "Colin's 2nd run: " & tEnd - tStart & " ms" & cr after msg
   if (tPeter2nd = tColin2nd) then
      put "Output Identical" after msg
   else
      put "ERROR!" after msg
   end if
end mouseUp



FUNCTION fRandomInRange pLower, pUpper
   return random(pUpper - pLower + 1) + pLower -1
end fRandomInRange




More information about the use-livecode mailing list