do loop speed up?



-----maxima-bounces at math.utexas.edu wrote: -----


>>?Mssq3?:?block?([msqsum,s1,s2,s3,s4,sL],?sL?:?[1,-1],
>>???????????msqsum?:?0,
>>???????????for?s1?in?sL?do
>>????????????for?s2?in?sL?do
>>?????????????for?s3?in?sL?do
>>??????????????for?s4?in?sL?do
>>???????????????msqsum?:?msqsum?+?Avsq?(factor?(emuon?(s1,s2,s3,s4))?),
>>???????????factorsum?(msqsum));
>
>Well,?a?"neater"?way?to?do?it?might?replace?the?do-loops?with
>an?iteration?over?the?result?of?cartesian_product:
>
>??S?:?{+1,?-1};
>??foo?:?cartesian_product?(S,?S,?S,?S);
>??bar?:?maplist?(lambda?([L],?apply?(emucon,?L)),?foo);
>??msqsum?:?apply?("+",?map?(Avsq,?map?(factor,?bar)));
>??factorsum?(msqsum);
>
>However,?my?guess?is?that?calls?to?factor?are?taking?most?of?the?time;
>I?doubt?if?the?nested?loops?are?slowing?down?the?program.

Right---I'd guess that all the sorting cartesian_product does makes this
approach slower.
Try something an experiment like: (I thought the number of calls to great
would be greater than 15 in %o16)

(%i14) (untimer(?great,?simplus,?simplifya), timer
(?great,?simplus,?simplifya))$
(%i15) Mssq3 : block ([msqsum,s1,s2,s3,s4,sL], sL : [1,-1],
          msqsum : 0,
          for s1 in sL do
           for s2 in sL do
            for s3 in sL do
             for s4 in sL do
              msqsum : msqsum + f(s1,s2,s3,s4),msqsum)$

(%i16) timer_info();
(%o16) matrix([function,time//call,calls,runtime,gctime],
              [simplifya,0,350,0,0],
              [simplus,0,16,0,0],
              [great,0,15,0,0],
              [total,0,381,0,0])

(%i17) (untimer(?great,?simplus,?simplifya), timer
(?great,?simplus,?simplifya))$
(%i18) (S : set(1, -1), xreduce("+",map(lambda([l], apply(f,l)),
cartesian_product(S, S, S, S))))$
(%i19) timer_info();
(%o19) matrix([function,time//call,calls,runtime,gctime],
              [simplifya,0,200,0,0],[simplus,0,1,0,0],
              [great,0,220,0,0],
              [total,0,421,0,0])

I'm not sure we win even if the cartesian product is pre-computed:

(%i20) (S : set(1, -1), r : cartesian_product (S, S, S, S))$
(%i21) (untimer(?great,?simplus,?simplifya), timer
(?great,?simplus,?simplifya))$
(%i22) xreduce("+",map(lambda([l], apply(f,l)), r))$
(%i23) timer_info();
(%o23) matrix([function,time//call,calls,runtime,gctime],
              [simplifya,0,187,0,0],
              [simplus,0,1,0,0],
              [great,0,152,0,0],
              [total,0,340,0,0])