Timing

Cputime() -> FldReElt

Return the CPU time (as a real number of default precision) used since the beginning of the Magma session. Note that for the MSDOS version, this is the real time used since the beginning of the session (necessarily, since process CPU time is not available).

Cputime(t): FldReElt -> FldReElt

Return the CPU time (as a real number of default precision) used since time \(t\). Time starts at \(0.0\) at the beginning of a Magma session.

Realtime() -> FldReElt

Return the absolute real time (as a real number of default precision), which is the number of seconds since 00:00:00 GMT, January 1, 1970. For the MSDOS version, this is the real time used since the beginning of the session.

Realtime(t): FldReElt -> FldReElt

Return the real time (as a real number of default precision) elapsed since time \(t\).

ClockCycles() -> RngIntElt

Return the number of clock cycles of the CPU since Magma’s startup. Note that this matches the real time (i.e., not process user/system time). If the operation is not supported on the current processor, zero is returned.

Time() -> MonStgElt

Return a string which should be assigned to a variable (say \(T\)) which can be passed to the next function after executing a series of statements.

Time(T): MonStgElt -> MonStgElt

Given a string \(T\) previously returned by the call Time(), return a string which gives the time elapsed since \(T\) was assigned. As for the time statement above, if a multi-threaded algorithm is used within the intervening statements at any point, and the real time \(r\) taken for all the statements is less than the CPU time \(c\) taken, then the resulting time is \(r\), and the tag [r] is appended (to indicate that the time printed is a real time); otherwise the resulting time is the CPU time \(c\). In this way, one can also tell whether a multi-threaded algorithm is actually used in the statements executed. See Example Timing Multithreaded Computations below.

time statement;

Execute the statement and print the time taken when the statement is completed. Since V2.25, if a multi-threaded algorithm is used within the executed statement and the real time \(r\) taken is less than the CPU time \(c\) taken, then the time printed is \(r\), and the tag [r] is appended (to indicate that the time printed is a real time); otherwise the CPU time \(c\) is printed (see example below). See Example Timing Multithreaded Computations below.

vtime flag: statement;
vtime flag, n: statement:

If the verbose flag flag (see the function SetVerbose) has a level greater than or equal to \(n\), execute the statement and print the time taken when the statement is completed. If the flag has level 0 (i.e., is not turned on), still execute the statement, but do not print the timing. In the first form of this statement, where a specific level is not given, \(n\) is taken to be 1. This statement is useful in Magma code found in packages where one wants to print the timing of some sub-algorithm if and only if an appropriate verbose flag is turned on. The behaviour when a multi-threaded algorithm is used is as above with the time statement.

SetShowRealTime(v): BoolElt

Sets whether or not to print real time as well as CPU time when a time or vtime statement is executed. By default this flag is false, meaning that only the CPU time is printed.

GetShowRealTime() -> BoolElt

Returns the current value of the flag that determines if real time is additionally printed by the time and vtime statements.

Example: Timing a Factorisation (ex-4cec25)

Run in calculator (opens in a new tab)

The time command can be used to time a single statement.

> n := 2^109-1;
> time Factorization(n);
[<745988807, 1>, <870035986098720987332873, 1>]
Time: 0.149

Alternatively, we can extract the current time \(t\) and use Cputime. This method can be used to time the execution of several statements.

> m := 2^111-1;
> n := 2^113-1;
> t := Cputime();
> Factorization(m);
[<7, 1>, <223, 1>, <321679, 1>, <26295457, 1>, <319020217, 1>, <616318177, 1>]
> Factorization(n);
[<3391, 1>, <23279, 1>, <65993, 1>, <1868569, 1>, <1066818132868207, 1>]
> Cputime(t);
0.121

We illustrate a simple use of vtime with vprint within a function.

> function MyFunc(G)
>    vprint User1: "Computing order...";
>    vtime  User1: o := #G;
>    return o;
> end function;
> SetVerbose("User1", 0);
> MyFunc(Sym(4));
24
> SetVerbose("User1", 1);
> MyFunc(Sym(4));
Computing order...
Time: 0.000
24

Run in calculator (opens in a new tab)

Example: Timing Multithreaded Computations (ex-de9abd)

Run in calculator (opens in a new tab)

In this example, we first time a matrix multiplication statement in the default single-threaded mode, so the time which is printed (or returned by the second Time call) is the CPU time.

> X := Random(MatrixRing(GF(5), 10000));
> time P := X*X; // single thread, normal CPU time
Time: 4.060
> T := Time(); P := X*X; Time(T); // similar with Time() function
4.060

Then we specify 8 threads for the multiplication algorithm, and note that the time has the tag [r] appended, to indicate that the time which is printed (or returned by the second Time call) is the real time.

> SetNthreads(8);
> time P := X*X; // multi-threads, so real time shown by [r]
Time: 1.050[r]
> T := Time(); P := X*X; Time(T); // similar with Time() function
1.050[r]

Run in calculator (opens in a new tab)