CTERM Download Article: Lila streake ★ Sender: Fork (Sand of Sahara Desert), Word Area: MATLAB Title: Accelerate MATLAB Running Triple Realm Servic Station: BBS Harbin Institute of Technology (Thu Jul 1 14:27:30 2004) Accelerate the triple realm of MATLAB, %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% 1, comply with Performance Acceleration rules II, abide by three rules three, no mention %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%% 1. Comply with Performance Acceleration rules About what is "Performance Acceleration" See Matlab Help file. I only briefly summarize its rules 7: 1. Only use the following data types, Matlab will speed up: logical, char, int8, uint8, int16, uint16, int32, uint32, double, if used in the statement Non-oriented data types do not accelerate, such as: Numeric, Cell, Structure, Single, Function Handle, Java Classes, User Classes, INT64, UINT642, MATLAB does not accelerate more than three-dimensional arrays. 3. When using the for loop, only the following rules will be accelerated: a, the scope of the FOR loop is only represented by scalar values; B, each statement inside the for loop must meet the above two rules, ie Only use the data type that supports acceleration, only the array of three-dimensional or less; c, only bundar-in function is called only in the loop. 4. When using IF, Elseif, While, and Switch, only scalar values are used in their conditional test statements, will be accelerated. 5, do not write multiple operations in one line, which slows down the running speed. That is, don't have such a statement: x = a.Name; for k = 1: 10000, sin (a (k)), end; 6, when a certain operation changes the data type or shape of the original variable (size, dimension) The running speed will be slowed down. 7. It should be used to use the recovery x = 7 2i, not such: x = 7 2 * i, the latter will reduce the running speed. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Second, follow three rules 1, try to avoid using loops, a document written in MATLAB "MATLAB is a matrix language, which means it is designedfor vector and matrix operations. You can often speed up your M-file code by usingvectorizing algorithms that Take Advantage Of this Design. Vectorization Means Convertingfor and while loops to equivalent Vector or Matrix Operations. There are two ways to improve such conditions: A. Try to replace cycle operations with a quantized operation. Such as will be below:
i = 0; for t = 0: .01: 10i = i 1; y (i) = sin (t); END replacement to: t = 0: .01: 10; y = sin (t); speed will Will get great accelerated. The most commonly used Vectorizing technologies include: All, Diff, Ipermute, Permute, Reshape, Squeeze, Any, Find, Logical, Prod, Shiftdim, Sub2ind, Cumsum, Ind2Sub, NDGRID, RepMAT, SORT, SUM, etc. Please note that there is such a document matlan supplement: "Before taking the time tovectorize your code, read the section on Performance Acceleration.You may be able tospeed up your program by just as much using the MATLAB JIT Accelerator instead ofvectorizing.". Where to go, I will grasp it. B. When the multiple cycle must be used, if the number of times the two cycles executes, the number of cycles is less in the loop, the inner ring performs a number of cycles in the outer ring of the cycle. This can significantly increase the speed. 2, a, pre-assigned matrix space, that is, determine the size and dimension of the variable in advance. This type of function has Zeros, Ones, Cell, Struct, RepMAT, etc. B. Use the repmat function to accelerate when you want to pre-allocate a non-Double type variable, such as following the following code: a = int8 (zeros (100)); replace: a = repmat (int8 (0), 100, 100); C. Use the repmat function when you need to expand a variable. 3, A, priority use the MATLAB built-in function, write time-consuming loops into the MEX-FILE to get acceleration. B. Use functions instead of Scripts. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Third, the trick You may think that the following is a fart, but sometimes it is the best way to solve the problem. 1. Use more efficient algorithm 2, using MEX technology, or use the tool provided by MATLAB to convert the program into C language, FORTRAN language. With regard to how to convert M files into C language programs, you can refer to this version post: "Summary: m file translate into C / C language file, VC compilation". ----------------------------------------------
From mathtools @
http://bbs.sjtu.edu.cn
http://genial.yculblog.com