en fr

# Time Functions

### clock

Current date and time.

#### Syntax

```t = clock
```

#### Description

clock returns a 1x6 row vector, containing the year (four digits), the month, the day, the hour, the minute and the second of the current local date and time. All numbers are integers, except for the seconds which are fractional. The absolute precision is plus or minus one second with respect to the computer's clock; the relative precision is plus or minus 1 microsecond on a Macintosh, and plus or minus 1 millisecond on Windows.

#### Example

```clock
1999 3 11 15 37 34.9167
```

### posixtime

Current Posix time.

#### Syntax

```t = posixtime
```

#### Description

posixtime returns the Posix time, the integral number of seconds since 1 February 1970 at 00:00:00 UTC, based on the value provided by the operating system.

#### Example

```posixtime
1438164887
```

clock

### tic

Start stopwatch.

#### Syntax

```tic
t0 = tic
tic(CPUTime=true)
t0 = tic(CPUTime=true)
```

#### Description

Without output argument, tic resets the stopwatch. Typically, tic is used once at the beginning of the block to be timed, and toc at the end. toc can be called multiple times to get intermediate times.

With an output argument, tic gets the current state of the stopwatch. Multiple independent time measurements can be performed by passing this value to toc.

By default, tic and toc are based on the real, "wall-clock" time. tic(CPUTime=true) is based on CPU time instead, which gives more accurate results for non-multithreaded programs. Measurements made with wall-clock time and CPU time are independent and can be mixed freely.

### toc

Elapsed time of stopwatch.

#### Syntax

```elapsed_time = toc
elapsed_time = toc(t0)
elapsed_time = toc(CPUTime=true)
elapsed_time = toc(t0, CPUTime=true)
```

#### Description

Without input argument, toc gets the time elapsed since the last execution of tic without output argument. Typically, toc is used at the end of the block of statements to be timed.

With an input argument, toc(t0) gets the time elapsed since the execution of t0=tic. Multiple independent time measurements, nested or overlapping, can be performed simultaneously.

With a named argument, toc(CPUTime=true) or toc(t0,CPUTime=true) use CPU time: toc measures only the time spent in the LME application. Other processes do not have a large impact. For instance, typing tic(CPUTime=true) at the command-line prompt, waiting 5 seconds, and typing toc(CPUTime=true) will show a value much smaller than 5; while typing tic and toc will show the same elapsed time a chronograph would. CPU time is usually more accurate for non-multithreaded code, and wall-clock time for multi-threaded code, or measurements involving devices or network communication.

#### Examples

Time spent to compute the eigenvalues of a random matrix:

```tic; x = eig(rand(200)); toc
0.3046
```

Percentage of time spent in computing eigenvalues in a larger program:

```eigTime = 0;
s = [];
tic(CPUTime=true);
for i = 1:100
A = randn(20);
eigT0 = tic(CPUTime=true);
s1 = eig(A);
eigTime = eigTime + toc(eigT0, CPUTime=true);
s = [s, s1];
end
totalTime = toc(CPUTime=true);
100 * eigTime / totalTime
78.4820
```