Time for action – using tic and toc

Let us first use tic and toc to get the wall-time for adding two matrices together the usual vectorized way:

octave:1> A=rand(1000,1000); B=rand(1000,1000);

octave:2> tic(); C=A+B; toc()

Elapsed time is 0.015 seconds.

It took 15 milliseconds to add two 1000 x 1000 matrices together, that is, to perform 1 million addition operations.

What just happened?

In Command 1, we declared two 1000 x 1000 matrices with elements picked from a uniform distribution having values between 0 and 1. In Command 2, we started the timer using tic, added the two matrices, and returned the elapsed time. We can assign the output from toc to a variable for later use. For example, Command 2 could have been:

octave:2> tic(); C=A+B; add_time ...

Get GNU Octave now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.