High quality C programming bit drop (2)
Use reasonable memory access
First, you need to open a memory to store and manage a matrix of 4 x 4, and unitize.
unreasonable:
Int Amatrix [4] [4];
For (int i = 0; i <4; i )
{
For (int J = 0; j <4; j )
{
IF (i == j)
{
Amatrix [i] [j] = 1;
}
Else
{
Amatrix [i] [j] = 0;
}
}
}
reasonable:
Int Amatrix [4 * 4];
For (int i = 0; i <4; i )
{
For (int J = 0; j <4; j )
{
IF (i == j)
{
Amatrix [i * 4 j] = 1;
}
Else
{
Amatrix [i * 4 j] = 0;
}
}
}
Analysis:
At any time, you must avoid using a multi-dimensional array, an increase in number of dimensions, and the corresponding program complexity will increase in geometric grade, and it is more difficult to understand.
Second, you need to assign a value to the above matrix, make it assign it from the upper right corner from the upper left corner.
unreasonable:
For (int i = 0; i <4; i )
{
For (int J = 0; j <4; j )
{
Amatrix [J * 4 I] = i * 4 j;
}
}
reasonable:
For (int i = 0; i <4; i )
{
For (int J = 0; j <4; j )
{
Amatrix [i * 4 j] = j * 4 i;
}
}
Analysis:
Try to ensure the order of access arrays of each element. Due to the management mode of Windows memory, memory is managed by paging. Order access arrays can basically ensure that the page will not switch back, thereby reducing the number of page failures and improves the overall performance of the program. This performance improvement is particularly obvious for large arrays.
Third, you need to use 3 FLOAT values to represent a three-dimensional point and write a function to calculate the array of three-dimensional points.
unreasonable:
Void foo (Float * Ppoints [3])
{
Float Apoint [3] = {1.0F, 2.0F, 3.0F};
INT ncount = (int) _msize (ppoints);
For (int i = 0; i { PPOINTS [I] [0] = apoint [0]; Ppoints [i] [1] = apoint [1]; Ppoints [i] [2] = apoint [2]; } } reasonable: Struct Point3 { Float X, Y, Z; } Void foo (Point3 * ppoints, int ncount) { POINT3 PT = {1.0F, 2.0F, 3.0F}; For (int i = 0; i { PPOINTS [I] = Pt; } } Analysis: There are two points. When designing such a function that needs to be passed into an array, don't forget that the number of elements of the array is also incoming, even if it is fixed, this will be a good habit. Second, for Float [3], try to avoid direct use, the best way is to use Struct to simply encapsulate it, and use "=" directly in copying. Fourth, you have a function definition, in which a relatively large object DATA is in this function, and delete it after calculation. But this function will be called frequently. unreasonable: Void foo (void) { Data * p = new data; Calcdata (p); Delete P; } reasonable: Char BUF [SIZEOF (DATA)]; Void foo (void) { Data * p = new (buf) data; Calcdata (p); } Analysis: New (buf) type; is a positioned new syntax, which does not real allocate memory, but simply divides a space that matches the type size at the specified memory start point, and directly on this memory This type is constructed, and the pointer to the object is returned. Since it has no real allocation of memory space, its efficiency is very high, in similar to the above routine, frequent application and release a large object, the positioned New can bring a large efficiency. (Serial)