Re: mu TECH: an unhurried digression on C pointers vs. arrays

From: Miguel Angel (maacruz@navegalia.com)
Date: Fri Apr 28 2000 - 17:58:21 CEST


On Fri, 28 Apr 2000, "Alfie Costa" <agcosta@gis.net> kindly wrote:
> Attached to this e-mail is a little benchmark program in C which times in
> seconds this lack of improvement or slowness. I've tested it with both Turbo
> C, (compiled for 8086s), and mu's own 'gcc'. Both compilers showed no
> advantages for pointers.
>
> A Dejanews search on usenet reveals that most everybody already knows that C's
> pointers aren't generally faster that arrays. Attached to this message are
> some of the more dismaying messages on the topic. (Zipped)
 
I think that worrying of speeding code written in C (or any other high level
language) changing such things like arrays for pointers, i=i+1 for i++, ... is
completely senseless.

The compiled code will be completely compiler (and machine deppendent) making
such efforts useless, and what is worse, offuscating code. If you still think
that you want a faster code under some system and compiler, you still will be
making asumptions about generated code.
First, lyou will have to earn how your code is compiled using a debugger where
you see the corresponding assembler (many surprises at this point), and you
will have to evaluate, in processor cycles, the real time ganancy (taking
account of pipelines, number of execution units, ...) for each loop cycle, and
estimate the total gained time.

If you really need speed, provided you are using the best known algorithm, you
will have to be very plattaform specific and use assembler (yet, the best
optimizing compiler is the human being). Do some profiling first, and write
highly optimized assembler versions of the inner loops, and keep using C or
PASCAL or whatever you use to the rest (I usually get from 50% to 100% of
improvement by this way)

About your test program, there is too much overhead on loops to really notice a
difference between using pointers or arrays (check this with turbo debugger).
To notice such subtle difference (if there is any at all) is better to write the
loop repeating many times the inner sentences (unless you are also testing the
loop).
        while( j<LEN )
               {
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
               line[j]=0;
               n=line[j++];
            ....
              }

  --
Don't see the world trought a window, be open{source}minded, and be free :-)

---------------------------------------------------------------------
To unsubscribe, e-mail: mulinux-unsubscribe@sunsite.auc.dk
For additional commands, e-mail: mulinux-help@sunsite.auc.dk



This archive was generated by hypermail 2.1.6 : Sat Feb 08 2003 - 15:27:14 CET