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 = toc() add_time = 0.015
Note
toc
returns the wall-time in resolution of milliseconds.
Vectorization
Let us, once more, illustrate the importance of vectorization. Instead of using the +
operator between the two matrices, we can add the elements...