[futurebasic] Re: [FB] initialize an array

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2006 : Group Archive : Group : All Groups

From: Stu Cram <stu@...>
Date: Wed, 4 Jan 2006 00:12:47 -0600
On Jan 3, 2006, at 9:16 PM, Steve Crossman wrote:
> Is there a quick way to initialize a variable array to zeros ?
> where for the variable array below, I can set the entire array to zero 
>
>  dim gtotals ( 50000 ) as double
>
> right now I do this
>  
> for x = 1 to 50000
> gtotals ( x ) = 0
> next x
>
> I may do this many times and I am trying to speed this up.
> should I use a dynamic array ?
>
> and one more thing.
>
> I want to find the fastest way to concatenate strings and ensure that 
> when adding them together, their length doesn't exceed 255 
> characters.  I know HOW to do this, but I am sure the way I am doing, 
> it is not the fastest method possible. 
>
> I need some Jay magic string handling here :)
> thanks
====================================

You can use FB's DEF BLOCKFILL( address of variable, # of bytes, byte 
value ) to quickly fill an section of memory like an array with the 
same value in its bytes. Here's a short demo that can be run in console 
mode to show the syntax. Note that arrays start with element zero which 
is ignored in the example.

dim gtotals(10) as double
dim x

text _courier,12

for x = 1 to 10
   print using "###.##  "; gtotals(x);
next x :print

for x = 1 to 10
   gtotals(x) = rnd(100)/10
next x

for x = 1 to 10
   print using "###.##  "; gtotals(x);
next x :print

DEF BLOCK( @gtotals(1), sizeof(double)*10, 0 )

for x = 1 to 10
   print using "###.##  "; gtotals(x);
next x : Print
================================================
Notes re parameters of DEF BLOCKFILL
1.  @ gtotals(1)
     This gives the address of of element 1 in the array gtotals()
     Use @ gtotals(0) to include the initial element too.
2.  sizeof(double)*10
     'sizeof' is a FB function that gives the # of bytes of a double 
type variable
     Multiply that by 10 elements (or 11 if including element 0).
     The result is the # of bytes to be filled.
3.  0
	This value is placed in each byte. As it happens, it results in a zero 
value
     in  each item of the array which for type double is 8 bytes.
     It's better to say 'double' than 8 since the value might be 
different on
     different processor (it used to be 10 bytes in early Macs for 
example).
4.  If the total number of bytes is going to be over 32767 (32K), then 
use the
     DEF LONGBLOCKFILL( ) version.
5.  A similar instruction, DEF CLEARHANDLE, can be used to put zeros in 
all the
     bytes referenced by the handle. It probably isn't to one to use 
here.
================================================

> I want to find the fastest way to concatenate strings and ensure that 
> when adding them together, their length doesn't exceed 255 
> characters.  I know HOW to do this, but I am sure the way I am doing, 
> it is not the fastest method possible. 

Perhaps if you were to send a sample of what you are doing now, it may 
be easier to optimize it.

Here's my suggestion using [0] notation to get the string lengths 
instead of LEN()
but not sure if it is most efficient.

LONG IF string1$[0] + string2$[0] <= 255  ' check total length
   result$ = string1$ + string2$
XELSE
   result$ = string1$ + LEFT$( string2$, 255-string1$[0] )
END IF

================================================
HTH