Re: Efficiency in accessing array elements

From: Grant Edwards (grante_at_visi.com)
Date: 04/30/04


Date: 30 Apr 2004 15:32:19 GMT

On 2004-04-30, Davide <ppp@ppp.it> wrote:

> Is it -in general- more efficient to assign, at the beginning
> of the operation, that element to a local variable (X:=A(K))
> to be used in the rest of the operation instead of using each
> time A(K)?

If you've got a decent compiler, there will be no difference:
the compiler will figure out what values get used most and keep
those values in CPU registers. OTOH, there are some crappy
compilers out there, so it may help to go the local variable
route. The only way to know is to try it and see. In reality
it probably just doesn't matter.

-In general-, write the function in the way that makes the code
easiest to read and maintain. Worry about efficiency of the
function _after_ you've got hard data showing you that your
system performance isn't good enough _and_ it's because of that
function.

-- 
Grant Edwards                   grante             Yow!  I'm a nuclear
                                  at               submarine under the
                               visi.com            polar ice cap and I need
                                                   a Kleenex!


Relevant Pages

  • Re: why still use C?
    ... > would get automatically if you removed the cast. ... malloc() to it's intended type ASAP is sound, ... the compiler is churning away, it does something with this cast, namely ... classify it as a decent compiler. ...
    (comp.lang.c)
  • Re: Why is there div() in the standard lib
    ... The div function provides that syntax. ... Any decent compiler should be able to notice you're dividing and ... Many machines operate with a minimal compiler, ...
    (comp.lang.c)
  • Re: Returning an array from a function
    ... Leor Zolman wrote: ... >>I doubt any decent compiler would copy the whole thing... ... The compiler could even avoid all copying by creating the local variable ... a.c.l.l.c-c++ FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html ...
    (alt.comp.lang.learn.c-cpp)
  • Re: puzzle
    ... > You failed to specify the compiler that chooses algorithms. ... > explain how constant subexpression evaluation affects the ... but he swears that "any decent compiler" will do it. ... "I don't really care about being right you know, ...
    (comp.programming)
  • Re: any regex gurus out there?
    ... > No matter what number or combination of backslashes I used it didn't work. ... - The compiler wouldn't take it otherwise. ... or escape each backslash with a backslash, ... > French language, the letter A may have three different accents. ...
    (microsoft.public.dotnet.languages.csharp)