9, programming Abas note nine code tuning
Even if action leads to mistakes, it also leads to learning and growth. Inaction is stagnation and atrophy.”
Welcome to reprint, reprint please indicate the source: blog.csdn.net/notbaron/ar…
Toad has little experience in code tuning. Occasionally, he helps colleagues tune scripts, but mostly he helps to realize functions and does not care about code optimization. As long as it can be done, why not wait longer, go to the bathroom, drink a glass of water, etc.?
We’ve learned from the previous chapters that code optimization can have a huge impact on performance, and proper code tuning can make a huge difference, so we’ll have to take a look at it later.
The author gives an example of optimizing a graphics processor, first through performance monitoring, and then discovering that it was the Malloc function that was taking up a lot of time. Then we looked at the memory allocator and found that the most common record types allocated 30 times more space than the next most common record types. The authors then take a bold approach to caching, caching common types of spatial records in a linked list. You can then handle common requests through quick access to the list without having to invoke a generic memory allocator.
Then the author gives four code tuning first aid solutions and one big trick.
Problem one: integer modulus. Because modular operations are 10 times slower than most arithmetic operations. Such as k = (j + rotdist) % n;
What if you use code to achieve modular operations?
Such as:
K= j+rotdist;
If( k >=n)
K -=n;
The successful modular operation is realized by other algorithms. Of course, the premise is that modulus operation is the bottleneck only effective.
Problem two: functions, macros, and inline code.
Macros can be used to maximize functions. Such as
#define max(a,b)((a)>(b)? (a):(b))
Problem three: sequential search. The sequential search itself is slow, and the author uses a method that makes toad see.
Figure 1
The loop contains only one increment, one array access, and one test. By expanding as above, each increment operation is deleted.
Problem 4: Calculating spherical distance.
Replace trigonometric functions by converting the dimension and longitude representations to x,y, and Z coordinates and calculating the sum of the squares of the differences between the three dimensions. Shorten the process from several hours to half a minute.
Then the author throws out the optimization process about binary search.
The most important rule of code tuning is to use it as little as possible. For code tuners, the author’s warning is “If you play with fire, be careful.”
The final rules are as follows:
Efficient handling of common situations
Use equivalent algebraic expressions
Use macro replacement functions to break up the function hierarchy
Use sentry to merge test conditions
Use equivalent algebraic expressions
The merge test condition reduces the number of array comparisons per inner loop from two to one; Using equivalent algebraic expressions, the representation of upper and lower bounds is transformed into the representation of lower limit and increment. Expand loop To expand the program to eliminate all loop overhead.