which is faster (a repeat structure question)
Richard Gaskin
ambassador at fourthworld.com
Thu Sep 26 03:46:00 EDT 2002
Shao Sean wrote:
> repeat with i = 127 to 255
> replace numToChar(i) with ("=" & toUpper(baseConvert(i,10,16))) in
> inputData
> end repeat
> return inputData
>
>
> repeat for each char inputDataChar in inputData
> if (charToNum(inputDataChar) >= 127) then
> put "=" & toUpper(baseConvert(charToNum(inputDataChar),10,16)) after
> outputData
> else
> put inputDataChar after outputData
> end if
> end repeat
> return outputData
>
>
> is "repeat for each" really that much quicker? i realize that the amount of
> data being converted is a major factor, but the first one only needs to loop
> 128 times whereas the second one has to loop for each char, which could be
> well more than 128 chars..
Try this:
on mouseUp
answer file "Select a file:"
if it is empty then exit to top
put url("file:"&it) into inputData
put len(inputData) into tLen
set the cursor to watch
--
-- TEST 1
--
put the milliseconds into tStart
repeat with i = 127 to 255
replace numToChar(i) with ("=" & toUpper(baseConvert(i,10,16))) \
in inputData
end repeat
put the milliseconds - tStart into tTime1
put inputData into tOut1
--
-- TEST 2
--
put the milliseconds into tStart
repeat for each char inputDataChar in inputData
if (charToNum(inputDataChar) >= 127) then
put "=" & toUpper(baseConvert(charToNum(inputDataChar),10,16)) \
after outputData
else
put inputDataChar after outputData
end if
end repeat
put the milliseconds - tStart into tTime2
put outputData into tOut2
--
-- RESULTS
--
if tOut1 <> tOut2 then put "error on one of the algorithms"
else put "Length: "&tLen&" replace: "&tTime1 &\
" repeat for each: "& tTime2
end mouseUp
I got these results testing on three different files:
Length: 2566 replace: 22 repeat for each: 5
Length: 16167 replace: 99 repeat for each: 35
Length: 204152 replace: 1726 repeat for each: 684
Perhaps on really large files the first method might be faster.
It takes only a moment to verify questions of relative performance, well
worth doing when you're looking for ways to optimize performance.
To see other performance comparisons and conveniently run your own try
MetaBench, a benchmarking tool for comparing script snippets:
<ftp://ftp.fourthworld.com/MetaCard/4W_MetaBench.mc.sit.hqx>
If you wanna see some really scary results, run the same test on equivalent
Mac and PC machines....
--
Richard Gaskin
Fourth World Media Corporation
Custom Software and Web Development for All Major Platforms
Developer of WebMerge 2.0: Publish any database on any site
___________________________________________________________
Ambassador at FourthWorld.com http://www.FourthWorld.com
Tel: 323-225-3717 AIM: FourthWorldInc
More information about the metacard
mailing list