: Is there any way that i can do the same thing without for loops?
: With for loops its very very slow, but when use something like
: array1:=array2
: its blazingly faster, but then you copy everything that array2 has,
: and I want NOT to copy the "empty" or " " characters (or any one
: else character that might be specified). Hope you understand what I
: am saying...
The only way I know is to do it in assembly, optimize it as much as possible and see if is there any speed improvement, but you have to have loops no matter what. Even a simple assumption like: array1:=array2 involves a loop on machine level. Including an example here, see if it helps:
{ Copy from "source" to "dest", "count" number of bytes }
{ "source" and "dest" should NOT overlap in memory }
procedure copyfast(source,dest:pointer;count:word);assembler;
asm
mov dx,ds { save data segment in dx }
mov cx,count { get count in cx }
les di,dest { es:di <-- dest }
lds si,source { ds:si <-- source }
shr cx,1 { cx:=cx div 2 }
cld { clear direction flag }
rep movsw { loop: copy one word fron source to dest }
adc cx,cx { leftover byte ? }
rep movsb
mov ds,dx { restore data segment }
end;
{ Selective copy from "source" to "dest", "count" number of bytes }
{ Will not copy the exemption values specified in "exempt" }
{ "source" and "dest" should NOT overlap in memory }
{ Returns the actual number of bytes copied }
function copyselective(source,dest:pointer;count:word;exempt:byte):word;assembler;
asm
mov dx,ds { save data segment in dx }
mov bl,exempt { get exemt in bl }
mov cx,count { get count in cx }
les di,dest { es:di <-- dest }
lds si,source { ds:si <-- source }
push dx { save dx in stack }
sub dx,dx { dx:=0 }
@1:
mov al,ds:[si] { get a byte from source }
inc si { increase source index }
cmp al,bl { compare byte with exempt }
je @2 { equal ? jump to @2 }
mov es:[di],al { not equal --> copy to dest }
inc di { increase destination index }
inc dx { dx:=dx+1 }
@2:
loop @1 { decrease cx, jump to @1 }
mov ax,dx { return no. of bytes copied }
pop dx { restore dx from stack }
mov ds,dx { restore data segment }
end;
type _array_=array[1..20] of byte;
var a1,a2:^_array_;
i:byte;
begin
new(a1);new(a2);
fillchar(a1^,sizeof(a1^),1); { fill a1 with 1's }
fillchar(a2^,sizeof(a2^),0); { fill a2 with 0's }
write('a1= ');for i:=1 to 19 do write(a1^[i],',');writeln(a1^[20]);
write('a2= ');for i:=1 to 19 do write(a2^[i],',');writeln(a2^[20]);
copyfast(a1,a2,20);
write('a2= ');for i:=1 to 19 do write(a2^[i],',');writeln(a2^[20],#13#10);
readln;
for i:=1 to 20 do a1^[i]:=i;
fillchar(a2^,sizeof(a2^),0); { fill a2 with 0's }
write('a1= ');for i:=1 to 19 do write(a1^[i],',');writeln(a1^[20]);
write('a2= ');for i:=1 to 19 do write(a2^[i],',');writeln(a2^[20]);
writeln(copyselective(a1,a2,20,5),' bytes copied'); { copy all, except value: 5}
write('a2= ');for i:=1 to 19 do write(a2^[i],',');writeln(a2^[20]);
readln;
dispose(a2);dispose(a1);
end.