use-revolution Digest, Vol 22, Issue 14

Alex Tweedly alex at tweedly.net
Mon Jul 4 18:24:46 EDT 2005


Malte Brill wrote:

> Hi Alex,
>
> here is the fastest we have been discussing in chatRev until now.
> ( chatrev.cjb.net )
> Thanks to kWeed, Mark and BvG!
>
> Still room for optimization though.
>
Thanks Malte (and others on chatrev).

Interesting approach to use lines instead of arrays - but I think that 
even with 768 lines, the searching for the lines adds up to more than an 
array lookup.  This version took 4 seconds on my sample photo.

I thought there was space for improvement in the bit that said :

>       if line newValue of newhistoGram is not a number then
>         put 1 into line (newValue) of newHistoGram
>       else
>         add 1 to line (newValue) of newHistogram
>       end if

You could save a condition test each time by initializing the whole list 
to 0
  repeat 768 time
     put "0" & cr after newHistogram
  end repeat

and then you could simply
   add 1 to line (newValue) of newHistogram
without the if test.

That brings it down from 4 seconds to 3.2  .....


But n fact, that's not as fast the version I posted last Thursday (see 
http://article.gmane.org/gmane.comp.ide.revolution.user/61547 for all 
those details).

Here's the current table of comparative results ....

Initial total     17 seconds    (including unnecessary copy into arrays)
Initial proper 17 seconds
chatrev           4 seconds
chatrev +       3.2 seconds
simple loop     2 seconds
really ugly        1.2 seconds

where the last two are from Thursday's email.
simple loop was simply

>>  repeat for each char c in lImageData
>>     add chartonum(c) to temp
>>     add 1 to count
>>     if count = 4 then
>>         add 1 to histo[temp]
>>     end if
>>     put 0 into temp
>>     put 0 into count
>>   end repeat 

The really ugly version did the same as this but instead of incrementing 
the histo array element every time, it incremented a char in a list  
(because we know that lookup of "char N of var" is much faster than 
either array lookup or "line N of var"), and when that reached 255, it 
got reset to 0 and the histo element updated accordingly). Really ugly - 
but 40% faster.

>   repeat for each char c in lImageData
>     add chartonum(c) to temp
>     add 1 to count
>     if count = 4 then
>       add 1 to temp   -- because 1:256, not 0:255
>       put numtochar(chartonum(char temp of mytemp) + 1) into char temp 
> of mytemp
>       if chartonum(char temp of mytemp) = 255 then
>         add 255 to histo[temp-1]
>         put czero into char temp of mytemp
>       end if
>       --      add 1 to histo[temp]
>       put 0 into temp
>       put 0 into count
>     end if
>   end repeat

>   repeat with i = 1 to maxHist
>     add chartonum(char i of mytemp) to histo[i-1]
>   end repeat



-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.8/37 - Release Date: 01/07/2005




More information about the use-livecode mailing list