In Borland Pascal you would use
fillchar(var x;value:word [size:word]);
so the invoking would look exactly like your example:
fillchar(x,0,sizeOf(x));
However with BP you can do it to the <64K in size variables (as this is a maximal size for "regular" variable in BP). Does this work for gpc as well?
George
Steve Loft wrote:
Is there a fast way to zero a large array, e.g. an equivalent to the following C fragment?
int x[100,100]; memset(x, 0, sizeof(x));
-- Steve
George Shapovalov wrote:
In Borland Pascal you would use
fillchar(var x;value:word [size:word]);
so the invoking would look exactly like your example:
fillchar(x,0,sizeOf(x));
However with BP you can do it to the <64K in size variables (as this is a maximal size for "regular" variable in BP). Does this work for gpc as well?
Yes (without the 64KB limit, of course).
However, the order of parameters is different (in BP and GPC):
procedure FillChar (var Dest; Count: Integer; Val: Char); or procedure FillChar (var Dest; Count: Integer; Val: Byte);
(At least that much is documented in the reference chapter in the manual, BTW.)
Frank
I wrote:
George Shapovalov wrote:
In Borland Pascal you would use
fillchar(var x;value:word [size:word]);
so the invoking would look exactly like your example:
fillchar(x,0,sizeOf(x));
However with BP you can do it to the <64K in size variables (as this is a maximal size for "regular" variable in BP). Does this work for gpc as well?
Yes (without the 64KB limit, of course).
However, the order of parameters is different (in BP and GPC):
Maybe that was unclear -- I didn't mean to say that it's different in BP and GPC, but rather it's the same in BP and GPC, but different from what was stated above. (Just pointing this out because there are prople who try to see incompatibilities between BP and GPC where there are none...). So the following syntax is for both BP and GPC:
procedure FillChar (var Dest; Count: SizeType; Val: Char); or procedure FillChar (var Dest; Count: SizeType; Val: Byte);
Frank
On Sunday 28 January 2001 10:09 pm, Frank Heckenbach wrote:
procedure FillChar (var Dest; Count: SizeType; Val: Char); or procedure FillChar (var Dest; Count: SizeType; Val: Byte);
Thanks (both of you). I tried that on an array[1..1000,1..1000] of integer, and the difference in time taken between
for i := 1 to 1000 do for j := 1 to 1000 do x[i,j] := 0;
and
fillchar(x, sizeof(x), 0)
was very small (the first took 18637 usec and the second took 17478 usec). But replacing sizeof(x) with 1000000 made it much faster (5348 usec).
So it seems that if you have to use sizeof, it's not really worth using fillchar. I'm a bit surprised by how expensive sizeof appears to be.
Do you have any feeling for how efficient the code produced by gpc is? Is it something you're looking to improve, or are you happy with it? I find that code compiled with gpc runs at about 75% of the speed of equivalent code compiled with gcc (using -O3 -march=i686 in both cases). Is this just a fact of life of using Pascal?
Steve Loft wrote:
On Sunday 28 January 2001 10:09 pm, Frank Heckenbach wrote:
procedure FillChar (var Dest; Count: SizeType; Val: Char); or procedure FillChar (var Dest; Count: SizeType; Val: Byte);
Thanks (both of you). I tried that on an array[1..1000,1..1000] of integer, and the difference in time taken between
for i := 1 to 1000 do for j := 1 to 1000 do x[i,j] := 0;
and
fillchar(x, sizeof(x), 0)
was very small (the first took 18637 usec and the second took 17478 usec). But replacing sizeof(x) with 1000000 made it much faster (5348 usec).
Well, the size of an Integer is 4 bytes, so the size of the array is 4000000. If you fill 1000000 bytes, you only fill 1/4 of it which is clearly faster. :-)
If the size of the array is known at compile time (i.e., no schema and no dynamic array), SizeOf is evaluated at compile time. In fact, you'll find that the executables produced with SizeOf(x) vs. 4000000 will be identical.
Do you have any feeling for how efficient the code produced by gpc is? Is it something you're looking to improve, or are you happy with it? I find that code compiled with gpc runs at about 75% of the speed of equivalent code compiled with gcc (using -O3 -march=i686 in both cases). Is this just a fact of life of using Pascal?
In general, most of the code produced is a good as that of gcc since they share the code generator. But in some areas, there are noticeable differences, especially the handling of strings is not quite optimal currently. We're hoping to improve this when we get around to it, but most of the time, feature requests and bug fixed have higher precedence...
Frank