[fpc-devel] Array indexing vs. pointers

Sven Barth pascaldragon at googlemail.com
Thu Nov 7 13:21:05 CET 2013


Am 06.11.2013 18:52, schrieb Jim Leonard:
> (Sorry if this isn't technical enough for the developer list, but it 
> requires understanding of the underlying architecture so I thought it 
> was appropriate.)  I'm implementing something in FPC that will perform 
> very many comparisons across sets of data, and would like to know 
> whether or not FPC is more efficient with array indexing calculations 
> vs. doing the pointer math myself.  For example, I have a choice of 
> doing either this:
>
>   for b:=0 to num-1 do
>     diff:=src^[b]-dst^[b];
Don't you mean "src[b]-dst[b]"? At least that would be the correct code 
if you'd use FPC's implicit array indexing.
>
> ...or this:
>
>   for b:=0 to num-1 do begin
>     diff:=src^-dst^;
>     inc(src);
>     inc(dst);
>   end;
>
> I read a few posts from 2010 and a post from 2013 that implies FPC's 
> array index calcs are just a MUL and suboptimial; is this still the 
> case?  Which method produces faster code?  A third way would be to 
> copy data into a dynamic array and no pointer use at all, ie. 
> "diff:=src[b]-dst[b];"... is this the fastest option?
The resulting instructions depend highly on the architecture. E.g. on 
x86 and m68k for the first case a MOV/MOVE instruction with a multiplier 
will be used while on other platforms this might involve an explicit 
multiplication. Also it will depend on the optimization level you use 
(e.g. on x86 the generated code for the first case is very different 
between -O2 and -O3). In the end you'd need to calculate the instruction 
timings to know whether one of the two is really faster than the other 
(and then you can compare that later on with your own implementation to 
check whether your code or the compiler's is faster ^^)

Regards,
Sven



More information about the fpc-devel mailing list