Very good point, Dan. While I wantid to come up with an equally witty pun, the challenge proved impossible…
Fair enough, NovaScotian - in which case the TID version of Dan’s suggestion might look something like:
to cutFirstCase of s from t
set d to text item delimiters
set text item delimiters to s
tell t to if (count text items) > 1 then set t to text item 1 & text from text item 2 to -1
set text item delimiters to d
t
end cutFirstCase
cutFirstCase of "cd" from "cdabcdefcdghcdijklmcd"
--> "abcdefcdghcdijklmcd"
In case they might help, here are a couple of additional variations:
to countAndCutCases of s from t
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set c to (count t) - 1
set text item delimiters to ""
set t to t as string
set text item delimiters to d
{c, t}
end countAndCutCases
countAndCutCases of "cd" from "cdabcdefcdghcdijklmcd"
--> {5, "abefghijklm"}
on indexList of s at t
set l to {}
set d to text item delimiters
set text item delimiters to s
repeat with n from 1 to (count t's text items) - 1
set l's end to (count t's text 1 thru text item n) + 1
end repeat
set text item delimiters to d
l
end indexList
indexList of "cd" at "cdabcdefcdghcdijklmcd"
--> {1, 5, 9, 13, 20}
Funnily enough, I considered including a brief description of the test script used - but wondered if that might perhaps be introducing too much noise. However, since you ask…
Some folks apparently use ‘current date’ to time scripts - although, since that gives results to the nearest second, it’s really a bit of a blunt instrument. For greater accuracy and convenience, consider using something more precise, such as Jon’s commands[1], GetMilliSec[2], Precision Timing Osax[3] or Smile’s ‘chrono’ [4].
[1] http://osaxen.com/files/jonscommands2.1.2.html
[2] http://osaxen.com/files/getmillisec1.0.1.html
[3] http://osaxen.com/files/precisiontiming1.0.html
[4] http://www.satimage.fr/software/en/index.html
To compare the performance of very fast routines, I usually place them inside a loop that repeats several (hundred/thousand) times (enough to achieve a clear and consistent difference). Since a run can sometimes throw up spurious results, I also run each test a number of times to establish a distinct pattern.
In addition, a certain amount of latency can occur during testing. This may, for example, extend the timing slightly on the first script run. One way around this is to reverse the order in which the scripts are run, and then to average out all the results.
To help focus on the essential differences between routines, it’s also a good idea to place any statements common to both (such as those initialising shared variables) outside the timed loops.
Having said all that, it may not be worth getting too hung up about minor timing differences - especially where a routine may be used only once within a script. (On the other hand, a script that iterates through hundreds or thousands of repeated operations may well benefit from some optimisation.) Remember, too, that performance can vary substantially from one machine to another - so it’s prudent to use caution when quoting any comparisons.
One final word of caution: Some third party scripting additions can enable certain operations and coercions that are not possible on a ‘vanilla’ system. To avoid confusion, it’s not a bad idea, after testing, to disable those that you don’t use regularly.
Anyway - here’s an example, based on earlier suggestions in this thread, using ‘the ticks’ from Jon’s commands:
set n to 100 (* number of repeats *)
set t to "cdabcdefcdghcdijklmcd"
set s to "cd"
set t1 to the ticks
repeat n times
set text item delimiters to s
set r to t's text items
set text item delimiters to ""
r as string
end repeat
set t2 to the ticks
repeat n times
do shell script "echo " & t & "| sed 's/" & s & "//g'"
end repeat
set t3 to the ticks
{tids:t2 - t1, shell:t3 - t2}
My apologies for this rather lengthy reply…