Question: execution time difference between seq and $ syntax

When the loop variable can be written as a unit step sequence, I never really distinguish between using

seq( f(i), i=m..n ), and

f(i) $ i=m..n

However I recent came across a case where the 'seq' construct ran about 2.5x faster. Is using 'seq' always faster? Does it depend on the function being evaluated? Why is there such a large difference in execution time

The original example which exhibited the problem is shown below, although after some experimentation, I have found other cases where 'seq' is faster (and plenty where it doesn't seem to make any difference!)

Example code for implementation using '$' is

restart:
ulim:=1000000:
t1:=time():
ans:= max
          ( { iquo(3*d, 7)/d $ d = 1..ulim }
             minus
            {3/7}
         ):
t2:= time()-t1;


Example code for for implementation using 'seq' is

restart:
ulim:=1000000:
t1:= time():
ans:= max
        ( { seq
            ( iquo(3*d, 7)/d, d=1..ulim )
          }
          minus
          {3/7}
        ):
t2:= time()-t1;

On my machine, the version using the 'seq' construct runs 2.5x faster

 

Please Wait...