From: Colin Paul Gloster on 10 Jun 2010 11:48 On Wed, 9 Jun 2010, Ludovic Brenta sent: |--------------------------------------------| |"[..] | | Two_Pi_Over_N : constant := 2 * Pi / N;| |[..]" | |--------------------------------------------| That is a good suggestion. |--------------------------------------------| |" FOR k in X'range LOOP" | |--------------------------------------------| No. |--------------------------------------------| |" FOR m in data'range LOOP | |[..]" | |--------------------------------------------| No. Loop down to zero. If utilizing FOR instead of WHILE, then utilize REVERSE. Additionally, loop unrolling should be considered.
From: Ludovic Brenta on 10 Jun 2010 10:54 On Jun 10, 5:48 pm, Colin Paul Gloster <Colin_Paul_Glos...(a)ACM.org> wrote: > On Wed, 9 Jun 2010, Ludovic Brenta sent: > > | Two_Pi_Over_N : constant := 2 * Pi / N;| > > That is a good suggestion. > > |" FOR k in X'range LOOP" | > > No. Why? > |" FOR m in data'range LOOP | > > No. Loop down to zero. Why? > If utilizing FOR instead of WHILE, then utilize REVERSE. Generally, FOR is much better than WHILE because, the instant you see FOR, you know the loop is finite; the moment you see WHILE you must look at the exit condition and the body of the loop before you know whether it terminates or not. > Additionally, loop unrolling should be considered. I agree with this but the solution would be less general. -- Ludovic Brenta.
From: Colin Paul Gloster on 10 Jun 2010 12:21 On Thu, 10 Jun 2010, Ludovic Brenta sent: |---------------------------------------------------------------------| |"On Jun 10, 5:48 pm, Colin Paul Gloster <Colin_Paul_Glos...(a)ACM.org> | |wrote: | |> On Wed, 9 Jun 2010, Ludovic Brenta sent: | |> | |> | Two_Pi_Over_N : constant := 2 * Pi / N;| | |> | |> That is a good suggestion. | |> | |> |" FOR k in X'range LOOP" | | |> | |> No. | | | |Why? | | | |> |" FOR m in data'range LOOP | | |> | |> No. Loop down to zero. | | | |Why?" | |---------------------------------------------------------------------| Though there should be no difference, in reality compilers produce less slow machine code when looping down to zero instead of up from zero. |---------------------------------------------------------------------| |"[..] | | | |> Additionally, loop unrolling should be considered. | | | |I agree with this but the solution would be less general." | |---------------------------------------------------------------------| True. Complementing Mathematica with Ada does not indicate that the source code in Ada is supposed to be general instead of fast. The strides in those loops are really indicative that the loops could be worth unrolling or running concurrently.
From: Adam Beneschan on 10 Jun 2010 13:37 On Jun 10, 9:21 am, Colin Paul Gloster <Colin_Paul_Glos...(a)ACM.org> wrote: > On Thu, 10 Jun 2010, Ludovic Brenta sent: > > |---------------------------------------------------------------------| > |"On Jun 10, 5:48 pm, Colin Paul Gloster <Colin_Paul_Glos...(a)ACM.org> | > |wrote: | > |> On Wed, 9 Jun 2010, Ludovic Brenta sent: | > |> | > |> | Two_Pi_Over_N : constant := 2 * Pi / N;| | > |> | > |> That is a good suggestion. | > |> | > |> |" FOR k in X'range LOOP" | | > |> | > |> No. | > | | > |Why? | > | | > |> |" FOR m in data'range LOOP | | > |> | > |> No. Loop down to zero. | > | | > |Why?" | > |---------------------------------------------------------------------| > > Though there should be no difference, in reality compilers produce > less slow machine code when looping down to zero instead of up from > zero. I don't think you can state that categorically. We don't even know what processor the OP was generating code for. Not all of them have "decrement and branch if nonnegative" instructions. In any event, I think the only way to really know which one is faster is to try it, or to examine the generated code---and the latter probably isn't good enough these days, with all the pipelining that goes on. -- Adam
From: Jeffrey R. Carter on 10 Jun 2010 13:57
Colin Paul Gloster wrote: > > Though there should be no difference, in reality compilers produce > less slow machine code when looping down to zero instead of up from > zero. The root of all evil. -- Jeff Carter "You a big nose have it." Never Give a Sucker an Even Break 107 |