Re: Operation recomputation




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 ;-)

.



Relevant Pages

  • Re: Operation recomputation
    ... I in a loop have to compare ... it everytime does the shifting from the original foo value or if it ... The answer depends on whether your compiler can recognize the common ... example, I use that 10 times for each pixel I process, and ...
    (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)
  • Re: Efficiency of code generated by Ada compilers
    ... an Ada compiler would introduce in your, ... Usually the type of pixel (grayscale, ... that matter in C++ there are template specializations, ...
    (comp.lang.ada)
  • Re: Operation recomputation
    ... in my case it's not a micro optimization since for my ... example, I use that 10 times for each pixel I process, and ... that's 60 million shifts a second I can avoid, I presume. ... Profile first, then optimize. ...
    (comp.lang.c)
  • Re: Fine tuning register use with C# ?
    ... it will be probably more efficient to pack the RGB values of a pixel ... Software Design Engineer, CLR JIT Compiler ... > smart the JITer is (optimizing the switch statements, ... >> But you HAVE to attach the process with debugging to see this - normal ...
    (microsoft.public.dotnet.framework.performance)