Re: Operation recomputation



Michel Rouzic wrote:
Nils O. Selåsdal wrote:
Michel Rouzic wrote:
Barry Schwarz wrote:
On 31 Aug 2006 20:04:02 -0700, "Michel Rouzic" <Michel0528@xxxxxxxx>
wrote:

Hi,

I'd like to know whether an operation which always has the same result
will be recomputed. Here's the context : I in a loop have to compare
many things to foo>>1 in an if statement. In that very if statement I
compare stuff with (foo>>1) about four times, and I'd like to know if
it everytime does the shifting from the original foo value or if it
re-uses the previous result, since the value of foo did not change.

Also I use many times in my loop (bar>>1), same question with it, does
it get calculated over again or not?

The answer depends on whether your compiler can recognize the common
sub-expression and the fact that the values have not changed. Some
compilers have options that say optimize for speed or optimize for
space. The language spec does not address the issue.

All in all a quality of implementation issue.
so, in case of a doubt, I should do the storage of the result myself,
like, make a variable to replace each recurrent operation?
Perhaps. But don't go turning every rock for potential
microoptimizations like this.

in my case it's not a micro optimization (I guess) since for my
(foo>>1) example, I use that 10 times for each pixel I process, and
since I can have up to 6 million pixels to process every second..
that's 60 million shifts a second I can avoid, I presume. Considered
that I'm going to run this code on a 200 MHz ARM920T, it might not be a
micro optimization at all ;-)

Don't presume.
Profile first, then optimize.
.



Relevant Pages

  • Re: Operation recomputation
    ... it everytime does the shifting from the original foo value or if it ... The answer depends on whether your compiler can recognize the common ... in my case it's not a micro optimization since for my ... example, I use that 10 times for each pixel I process, and ...
    (comp.lang.c)
  • Re: Operation recomputation
    ... I in a loop have to compare ... it everytime does the shifting from the original foo value or if it ... Also I use many times in my loop, same question with it, does ... The answer depends on whether your compiler can recognize the common ...
    (comp.lang.c)
  • Re: Operation recomputation
    ... I in a loop have to compare ... it everytime does the shifting from the original foo value or if it ... Also I use many times in my loop, same question with it, does ... The answer depends on whether your compiler can recognize the common ...
    (comp.lang.c)
  • Re: Operation recomputation
    ... I in a loop have to compare ... it everytime does the shifting from the original foo value or if it ... Also I use many times in my loop, same question with it, does ... The answer depends on whether your compiler can recognize the common ...
    (comp.lang.c)
  • Re: code optimization
    ... > void myrotate(int dim, pixel *src, pixel *dst) ... the compiler that the two ranges don't overlap. ... Hmm, not much that can be done here. ... to the row in the inner loop in order not to redo this computation for ...
    (comp.lang.c)