Surprise at the end of the article, welcome to pick it up! Can be directly drawn to the end of the article!


Don't miss the prize!


As long as the relevant product is purchased in a retail store, it must be activated. So don't give too much introduction.


Features: genuine, paid, after-sales service, permanent activation



1. First learn to use profiler to analyze code efficiency.


1.1. Open the profiler.


On my machine: under matlab desktop, Desktop->Profiler.


Under the M file editor, Tools->Open Profiler.


1.2. Running the profiler


You can copy the code to be run into the input box after Run this code.


You can run this example


[t,y] = ode23('lotka',[0 2],[20;20])

You can also enter the name of the M file to run.

1.3.Click Start Profiling (or press Enter after entering the statement).

1.4. View Profile Detail Report

       It will tell you which code is consuming how much time, you can find which functions or which lines of code are consuming the most time, or are being called frequently.

You can also use the stopwatch Timer function to calculate the program consumption time

Use tic and toc as shown here.

tic

   - run the program section to be timed -

toc

2. Acceleration method 1 - vectorized programming

     

   MATLAB programs are interpretted. This would seem to make it inapproapriate for large scale scientific computing. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize \"vectorization\". The concept of vectorization is central to understanding how to write efficient MATLAB code.

     Vectorized code takes advantage, wherever possible, of operations involving data stored as vectors. This even applies to matrices since a MATLAB matrix is ​​stored (by columns) in contiguous locations in the computer's RAM. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used.

       MATLAB is a matrix language, which means it is designed for vector and matrix operations. You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.

i = 0;

for t = 0:.01:1000;

    i = i+1;

    y(i) = sin(t);

end

The runtime is 30.776 seconds.

         This code is a natural conversion from the C language form, but its efficiency is very low! Matlab allocates memory for variables in real time. During the first loop (that is, when i=1), Matlab allocates memory for the y vector (length 1). Each time the loop is executed in the future, Matlab will append a new element to the end of y. This not only leads to an increase in calls to allocate memory, but also makes the distribution of elements of y in memory not contiguous (like the difference between an array and a linked list in a data structure). In contrast, the following code is much more efficient:

Change to vectorized code:

t = 0:.01:1000;

y = sin(t);

The runtime is 0 seconds.

      The first statement allocates a contiguous memory space to store a vector t with multiple elements. Similarly, when the second statement allocates memory, it also allocates a contiguous memory space to store the vector y with multiple elements. Aside from the cost of calculating sin, the number of executions of memory allocation commands and the ease of accessing vector elements are indistinguishable.

Functions Used in Vectorizing, Some of the most commonly used functions for vectorizing are: all, diff, ipermute, permute, reshape, squeeze, any, find, logical, prod, shiftdim, sub2ind, cumsum, ind2sub, ndgrid, repmat, sort, sum

 

3. Acceleration method 2 - Preallocating Arrays (pre-allocated space)

    You can often improve code execution time by preallocating the arrays that store output results. Preallocation makes it unnecessary for MATLAB to resize an array each time you enlarge it. Use the appropriate preallocation function for the kind of array you are working with.

Preallocation also helps reduce memory fragmentation if you work with large matrices.

       Although Matlab will automatically adjust the size of variables, it is better to allocate memory space for variables in advance. Because this can reduce the number of times the memory allocation command is called to 1, and can also make variables stored continuously in memory (when the variable is a matrix, it is stored continuously in memory by column). The so-called "pre-allocating memory space for variables" means that when the size of the variable is known, before any element in the variable is not referenced, a variable with the same size is created.

Follow and reply to [software] to get it for free!



Please send a message to [Little Sweet] from Jiangsu , who won the prize at the end of the last issue, and provide the courier address. The prize will be mailed within one week after receiving the address. Those that cannot be shipped temporarily due to the epidemic will be shipped as soon as possible after the unblocking .


🎁The draw at the end of the article🎁

Retweet this article to say your blessings


Pull 5 treasures to take the heart, practical high-end natural & salt stone lamp

picture

Selection conditions:

1-Follow this official account


2-Retweet this tweet to the circle of friends. You cannot only see it yourself. You need to keep it until you check it. You cannot take fake screenshots.


3- After the lottery is drawn, please check it within 3 working days. If the award is not redeemed within the time limit or does not meet the requirements, the award will be disqualified.

After paying attention to this public account, you can reply to [Lottery Draw]



Thank you all for reading, watching and forwarding all the time, 1000T super exquisite learning materials are given to you! Please get it by yourself


English vocabulary must memorize 3500 syllabus vocabulary list to memorize artifact out of order version of the new class standard mind map diagram clever shorthand junior high school high frequency words Daquan

                                                      

Lucky Wheel Lucky Wheel Surprise Event  


                                                      picture