Richard Robinson on Sun, 06 Dec 2015 18:17:47 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: time function inside loop


Most excellent!  Thanks!  It works now.

Richard

On Sun, Dec 6, 2015 at 1:16 AM Karim Belabas <Karim.Belabas@math.u-bordeaux.fr> wrote:
* Richard Robinson [2015-12-06 07:54]:
>
> This is a really simple question.
>
> Is it possible to get the time function to work inside of a loop?
>
> I can get it to work outside of one but I just get various syntax errors
> when I try it inside.
>
> for (i=6,887, \
> tot= 23 * 76 * 76 * 654 * 6543 + i; \
> #
> );
> #
> quit

Use the gettime() function :

{
  gettime(); /* reset timer */
  for (i=6,887,
    tot= 23 * 76 * 76 * 654 * 6543 + i;
    print(gettime());
  );
  quit
}

(the enclosing braces are only here to avoid the trailing \ after each line)

N.B. 1) '#' is a "shortcut" that switches the timer on and off and only
works as a single statement in the gp interpreter (like \r and friends
mentionned in ?\): it cannot be combined with other statements.

2) The "time" function is gettime(), that returns the number of
miliseconds since the last call to itself.

3) If you really want the effect of '#' itself (switch timer on and off)
and not the elapsed time, the corresponding command would be
  default(timer, 1)  \\ switch timer on; replace 1 -> 0, to switch it off

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405
Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`

--
Richard Robinson