## `rtime, time` -- measure real time and execution time

#### Introduction

`rtime()` returns the total real time in milliseconds spent in the current session.

`rtime(a1, a2...)` returns the real time needed to evaluate all arguments.

`time()` returns the total execution time spent in the current session.

`time(a1, a2...)` returns the execution time needed to evaluate all arguments.

#### Call(s)

`rtime()`
`rtime(a1, a2...)`
`time()`
`time(a1, a2...)`

#### Parameters

`a1, a2...` | - | arbitrary MuPAD objects

#### Returns

a nonnegative integer

#### Related Functions

`prog::profile`

#### Details

- `rtime` returns the time in milliseconds. Note, however, that at present the last three digits are always 0, i.e., the precision of the time measured by `rtime` is only one second.
- The result of `time()` comprises all the computation time spent by MuPAD. This includes the time for system initialization and reading input (parsing). However, it excludes the time spent by other programs running at the same time, even if they were started by a `system` command.

- The time returned by `time` is computed in a system-dependent way, usually counting the number of clock ticks of the system clock. Hence, the time returned by `time` is a multiple of the system's time unit and cannot be more precise than 1 unit. E.g., the time unit is 10 milliseconds for many UNIX systems.
- On computers without ``time-sharing'', such as the Macintosh, real time and CPU time roughly coincide.
- `rtime` and `time` are functions of the system kernel.

#### Example 1

This example shows how to do a time measurement and assign the computed value to an identifier at the same time. Note that the assignment needs extra parenthesis when passed as argument:

    >> rtime((a := int(exp(x)*sin(x), x)))

                                       9000

    >> a

                           sin(x) exp(x)   cos(x) exp(x)
                           ------------- - -------------
                                 2               2

    >> delete a:

Alternatively, one may time groups of statements in the following way:

    >> t0 := rtime():
       command1
       command2
       ...
       rtime() - t0

#### Example 2

Here we use `rtime` to compute the elapsed hours, minutes and seconds since this session was started:

    >> t := rtime()/1000:
       h := trunc(t/3600):
       m := trunc(t/60 - h*60):
       s := t - m*60 - h*3600:

    >> print(Unquoted, "This session is running for " .
                       h . " hours, " .  m . " minutes and " .
                       s. " seconds.")

          This session is running for 0 hours, 0 minutes and 10 seconds.

    >> delete t, h, m, s:

#### Example 3

To obtain a nicer output, the measured time can be multiplied with the appropriate time unit:

    >> time((a := isprime(2^1000 - 1)))*msec

                                     700 msec

    >> time((a := isprime(2^1000 - 1)))*sec/1000.0

                                      0.7 sec

    >> delete a:

#### Background

- On a UNIX system, the time is measured using a system call to the function `time` on that system.

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000