Inefficient code - Solutions
BNig
niggemann at uni-wh.de
Tue Jun 30 16:27:46 EDT 2009
Bill,
Bill Marriott wrote:
>
> Can anyone improve on it?
>
well, only Bill can. I used your logic and added a forward search when the
comparison is not unequal, figuring that going forward yields faster
results. Well it kind of does, if one thinks 5 to 10 milliseconds are an
improvement...
I would not have posted it if you hadn't asked.
the change is marked in the code.
this code is working code in Bill's stack.
-------------------------------------------
on mouseUp
put the imagedata of image "Alpha" of group (the activeTab of btn "Image
Style") into ImageA
put the imagedata of image "Beta" of group (the activeTab of btn "Image
Style") into ImageB
set the endvalue of scrollbar "Progress" to \
the length of imagedata of image "Alpha" of group (the activeTab
of btn "Image Style")
put 0 into currPixel
put the length of ImageA into dataLength
put dataLength into rangeToCheck
put 4 into tIncrement
put the milliseconds into t
-- begin timing
repeat while currPixel < dataLength
-- starting with the whole image, check a range of pixels for
differences
-- keep slicing the range in half until we find a block of unchanged
pixels
repeat while char currPixel+1 to currPixel+rangeToCheck of ImageA <> \
char currPixel+1 to currPixel+rangeToCheck of ImageB
-- aha, the range we're testing has changes
if rangeToCheck >= 8 then
--breakpoint
-- eight bytes is at least two pixels... it's still too big;
slice it in half
put rangeToCheck div 4 div 2 * 4 into rangeToCheck
else
-- we're down to a single changed pixel now
-- record which pixel has changed (offset within the imageData)
put 1 into bytesChanged[currPixel+1]
-- move to the next pixel; assume that changed pixels are near
each other
add 4 to currPixel
end if
end repeat
-- we found one or more unchanged pixels; skip this section of data
add rangeToCheck to currPixel
-- this is added to Bill's original code
-- assume there are more unchanged pixels and go forwards
repeat while char currPixel+1 to currPixel+tIncrement of ImageA = \
char currPixel+1 to currPixel+tIncrement of ImageB
add tIncrement to currPixel
add tIncrement to tIncrement
if tIncrement > dataLength then exit repeat
end repeat
-- end addition to Bill's code
-- and update the range to encompass the remainder of the image
put dataLength - currPixel into rangeToCheck
end repeat
-- end timing
put the milliseconds - t into d
put the keys of bytesChanged into t
sort t numeric
put t into field "diff"
set the thumbposition of scrollbar "Progress" to the endValue of
scrollbar "Progress"
put the number of lines in fld "Diff" && "pixels changed," && d &&
"milliseconds"
end mouseUp
------------------------------------------
as usual, watch out for linebreaks
regards
Bernd
--
View this message in context: http://www.nabble.com/Inefficient-code-tp24226458p24278988.html
Sent from the Revolution - User mailing list archive at Nabble.com.
More information about the use-livecode
mailing list