我们编写的有限元计算程序,解决具体问题往往需要大量运算时间。如果说计算一分种和计算两分钟相差不大,那么计算半天时间和计算五天时间就差别很大了。
在许多论文里看到,以下这些技巧,综合起来可以使原本五天完成的任务加快到一天内完成,并且实现起来也很容易,且一般不破坏程序的可读性。因此对于编程者来说是有必要了解的。
Real time programming approaches:
1) avoid floating point numbers as much as possible:
x*y takes almost ten time longer if a and y are floating point numbers than if they are integers. If a parameter has float value, we can use scale-up integer to represent it as much as possible.
2) avoid function calls as much as possible;
calling a function takes extra computing resources because stack pop and push of the function parameters and returns. Short functions can be implemented using predefined macros. If function is necessary, we can reduce the parameter list at the cost of less safety when using more global variables.
3) define a union for 2D array
union in C defines alias of data type. We define a union of a 2D array and a 1D array with the same size.
union TackUnion
{
int array_2D[Width][Height];
int array_1D[Width*Height];
} TackName;
This is very useful in reducing computing time when clearing the 2D array.
for (int m=0;m<Width*Height;m++)
{ array_1D[m] = 0;}
Another way is to get the address of a 2D array, which is an integer multiplication and summation.
2 j+ U+ ]" l0 G, f( n: i6 w
4) Use time efficient operator:
Less efficient Time efficient
m+1 m++
2*x x<<2
5*x x<<2+x
a*x*x+b*x+c (a*x+b)*x+c
x=x+y x+=y
5) define register integer variables as array index
We can use register integers as array index; however we can not declare all integers as register integers because of the very limited number of CPU registers.
6) the unrolling technique
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删