Is there a faster way?

Jan Schenkel janschenkel at yahoo.com
Sun Jun 29 11:12:00 EDT 2003


--- "T. R. Ponn" <alptex2 at orwell.net> wrote:
> Hello,
> 
> I have a binary file that I want to divide up
> between 2 variables, 
> depending on user input.
> 
> With memBank value "01"
> a file like this: "12345678"
> would end up: "1357" in variable bank0 and "2468" in
> bank1.
> With memBank value "10" the same file would end up:
> "2468" in variable 
> bank0 and "1357" in bank1
> 
> The file can be sizable (maybe 128MB), though far
> more typical would be 
> around 8 MB.  The following code snippet
> accomplishes the feat in about 
> 25 seconds for a file size of 8MB...using my
> G4/500MHz, OS 9.2.2 and 
> Rev1.1.1.
> 
> on eenieMeenieMinieMo fileContents
>   put the length of fileContents into fileLength
>     if char 1 of memBank=0 then  --memBank="01", so
> start with even bank
>       repeat with b=1 to ((trunc(fileLength/2))+1)
>         put char ((b*2)-1) of fileContents after
> bank0
>         put char (b*2) of fileContents after bank1
>       end repeat
>     else  --memBank="10", so start with odd bank
>       repeat with b=1 to ((trunc(fileLength/2))+1)
>         put char ((b*2)-1) of fileContents after
> bank1
>         put char (b*2) of fileContents after bank0
>       end repeat
>     end if
> end eenieMeenieMinieMo
> 
> Size of code is of no consequence (I've already cut
> the length in 
> half...it's just easier to understand what I'm
> trying to accomplish in 
> this form), but speed is.  Any ideas on how to speed
> this up?  The 
> utilization of arrays seemed to have little effect
> on it.  Thanks for 
> any help you can lend!
> 
> Best Regards,
> 
> Tim Ponn
> 


Hi Tim,

In this case, 'repeat for each' might be your friend :
it usually cuts down on the number of times the engine
has to go through the variable to pluck data from it.
Try the speed of this :

on eenieMeenieMinieMo pFileContents
  put char 1 of memBank into tCurrentBank
  repeat for each char tChar in pFileContents
    if tCurrentBank = 0 then
      put tChar after bank0
      put 1 into tCurrentBank
    else
      put tChar after bank1
      put 0 into tCurrentBank    
  end repeat
end eenieMeenieMinieMo

I haven't compared speeds, but I think this version
might work faster because it doesn't have to
recalculate b*2+1 etc eery iteration.

Hope this helped,

Jan Schenkel.

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com



More information about the use-livecode mailing list