The “Exponent Test” takes the number 3 to the power of an array of random numbers and saves the results in a new array. ![]() The second test was named the “Logarithm Test” and computed the natural logarithm of a 2-D array of random numbers. The first test was named the “Polynomial Test” and involved adding the first through fourth powers of a list of random numbers. I created six tests that represent common tasks in Matlab. For tests that used random numbers, I set the random number generator to the same state so that the two versions of code use the same random numbers. I also used Monte Carlo simulations with 1000 iterations to obtain more precise values for the ratios between the two versions of the code. Preallocation decreases running time by a tremendous amount and is necessary for any tasks that are remotely time-sensitive. I always preallocated arrays for the non-vectorized code, to make the comparisons fair. Tests involving mathematical operations ran faster using vectorized code, while tests involving conditional operators and vector creation ran faster for non-vectorized code.įor each task, I created a non-vectorized and a vectorized version of code. The results were mixed, showing a decrease in running time for vectorized code on some tasks and a increase in running time on other tasks. I created tests for six tasks and compared the amount of time needed to run the vectorized and non-vectorized code. However, vectorization is not always the faster alternative for time-sensitive tasks. ![]() ![]() If you have to do a large data processing task or need to create real-time application that does a large number of mathematical operations, vectorization is often a good option. Vectorization allows a programmer to write code that is more intuitive, more concise, and often faster than using standard for, while and if statements. What separates Matlab from many other programming languages is the ability to vectorize code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |