3.5 efficiency of the object Member (object Mem ber efficiency)
One of the following tests is intended to test aggregations (aggregation). Encapsulation (encapsulation), and the degree to which the inheritance (inheritance) is caused by the additional load. All tests are added, subtracted, and assigned to individual local variables (assign) Access costs for the operation. The following are the individual local variables:
float pa_x = 1.725, pa_y = 0.875, pa_z = 0.478;
float pb_x = 0.315, pb_y = 0.317, pb_z = 0.838;
Each expression needs to be executed 10 million times, as follows:
for (int iter = 0; iter < 10000000; iter++) {
pb_x = pa_x-pb_z;
pb_y = pa_y + pb_x;
Pb_z = Pa_z + pb_y;
}
The first test is for a local array of three float elements:
Enum fussy{x, y, z};
for (int iter = 0; iter < 10000000; iter++) {
PB[X] = pa[x]-pb[z];
Pb[y] = Pa[x] + pb[x];
PB[Z] = Pa[z] + pb[y];
}
The second test is to convert the same array elements into a c struct data abstract type, where the members are float and the member names are X, Y, Z:
for (int iter = 0; iter < 10000000; iter++) {
pb.x = pa.x-pb.z;
PB.Y = Pa.y + pb.x;
Pb.z = pa.z + pb.y;
}
The deeper abstraction is to make the data encapsulation and use the inline function. The coordinate point is now represented by a separate Point3D class. Try two different forms of access function, first, define an inline function, return a reference, Allows it to appear at both ends of the assignment operator:
Class Point3D {
Public
Point3D (float xx = 0.0, float yy = 0.0, float zz = 0.0)
: _x (XX), _y (yy), _z (ZZ) {}
Float &x () {return _x;}
Float &y () {return _y;}
Float &z () {return _z;}
Private
Float _x, _y, _z;
};
Then the actual access to each coordinate element should look like this:
for (int iter = 0; iter < 10000000; iter++) {
pb.x () = pa.x ()-pb.z ();
PB.Y () = PA.Y () + pb.x ();
Pb.z () = PA.Z () + PB.Y ();
}
The second form of access function defined is the provision of a pair of Get/set functions:
float x () {return _x;}
void X (float xx) {_x = xx;}
So the access operation for each coordinate value should look like this:
Pb.x (pa.x ()-pb.z ());
The results of these various tests are given below (when the optimization switch is turned on, the "package" will not bring the efficiency cost of the execution period, do not know how to open the optimization switch ...) Forget it)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The efficiency of the C + + object Model object Member (object Mem BER Efficiency) (chapter III).