Join Patrick Royal for an in-depth discussion in this video Performance considerations, part of Learning MATLAB.
Computers today are extremely fast and they will undoubtedly get exponentially faster in the future. Despite this, computing time is still a finite resource. And it saves time and money to design programs that run as quickly and efficiently as possible. In general, computing time is displayed in big O notation, which is a quick and dirty representation of the rate at which the number of calculations scales with the data set size. For instance a program with o of n run time would take twice as long to process a data set with twice as many values.
While a program with O of n squared, would take two squared, or four times as long, to process the same double data set. The steepest scaling, is exponential, such as, O of two to the n. This means that calculations would take twice as long for a data set with just one more value, making the program only feasible to run for extremely small amounts of data. The time of function in MATLAB use the Tic and Toc functions. The Tic function starts a built-in stop watch function that measures computing time to the millisecond.
While the Toc starts the function and returns the amount of time elapsed. The general syntax for these functions, is to put tic on the line before whatever part of the function you want to time and then store the output of talk in a permanent variable by typing the variable name equals talk. The stopwatch can be used multiple times and assigned to multiple variables. So it's easy to time each critical part of the function and record the execution time of each one. As an example, we can use the practice script that you created in a previous video.
So, I'll open that, and if you wanted to time this function, on the first line, we'd put a tic function. And then on the last line we would put a toc function. Then when we run, we could see that it would now display that the elapsed time is .024 seconds. Optimizing functions themselves to reduce their big O value is an extremely complicated subject, and beyond the scope of this course. But there are several coding practices in MatLab that can significantly reduce the time a program takes to run. First, be sure to put semicolons after every line that doesn't have output that is absolutely necessary for the user to see.
Writing output to the command window slows down the execution of the program substantially, and simple calculations can take an inordinate amount of time. This is especially important in loops or when dealing with matrices with hundreds of thousands of values. The second good practice is to allocate a variable only once. It takes far less time for MATLAB to define a 100 by 100 matrix of 0's, and then fill it cell by cell than it takes for MATLAB to define a 1 by 1 matrix then extend to 1 by 2, then 1 by 3, and so on all the way up to 100 by 100. The 0's and 1's functions are useful here since they allow you to set the dimensions of a matrix and fill it with placeholder data. Going back to the practice script, if we wanted to store all the stock data in a single matrix, we would start by defining the matrix on the line above, stock, data, equals 0's of 10,1.
And then within the loop you would stay stock data of I plus 1 comma equals E to means. This would ensure that matrix is defined only once, which makes it much faster for MATLAB to run. Finally, be sure not to make MATLAB do any more calculations or hold any more data than necessary to get the answer. It's easy to just make every variable a double or an unsigned integer of 64 bits, but this wastes a lot of space, and it forces MATLAB to do calculations on far more digits than is necessary in most cases.
Symbolic arithmetic is even worse for speed. While it will completely eliminate rounding errors, it also takes many times longer to run than normal arithmetic. Variables can be left as high precision values while you're writing the script to avoid any rounding errors while debugging, but once the program is complete, it can significantly reduce the run time to go back and change variable to single or 8 bit integer wherever higher degree of precision are unnecessary.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox