Home > Article > Backend Development > C++ performance analysis (4): The impact of inheritance on performance, analysis of inheritance_PHP tutorial
(This editor has a problem today and messed up my format. Sorry ! )
Inheritance is an important feature of OOP. Although many colleagues in the industry do not like inheritance, using inheritance correctly is an important design decision at the application level and architecture level. What impact will extensive use of inheritance, especially in std containers, have on program performance?
From my personal experience, constructor has a great impact on creating classes with deep inheritance chains. If the application allows it, it is best to use a base class without a constructor. Here’s an example:
struct __declspec(novtable) ITest1
{ virtual void AddRef() = 0;
virtual void Release() = 0;
virtual void DoIt(int x) = 0; };
class CTest: public ITest1
{
int ref;
public: inline CTest() { ref = 0; }
inline void AddRef() { ++ref; }
inline void Release() {--ref; }
inline void DoIt(int x) {ref *= x; }
inline void AddRef2() { ++ref; }
inline void Release2() {--ref; }
inline void DoIt2(int x) {ref *= x; }
static void TestPerf(int loop); };
This is a dummy program, but it is very common in COM. If we are going to create and use CTest in large quantities, experienced programmers should see that ITest1 does not need a constructor at all. According to the C++ manual, ITest1 is a "non-simple constructor class" because it has virtual functions. Compilation must generate a constructor, whose only purpose is to set ITest1's vtbl (virtual function table).
However, the only function of interface is to be inherited, so its vtbl must be set by its inherited class. The compiler does not need to generate a constructor in this case. Microsoft recognized this when designing ATL and launched its own solution to avoid the flaws of C++'s official SPEC: VC++ provides novtable's class modifier , tells the compiler: I don’t need your constructor. However, my test results in VS 2010 were disappointing:
The constructor ofITest1 is still generated, but it does not assign a value to vtbl. This does nothing to improve the performance of base class construction. Let's take a look at the impact of this "useless constructor" on performance. Let's take another ITestPOD that does not require virtual functions (POD means "data only") for comparison:
struct ITest1POD
{ inline void AddRef() { }
inline void Release() { }
inline void DoIt(int x) { } };
Of course ITestPOD cannot be fully used as an interface (interface must use virtual functions), it is just for testing. Then, we design an inherited class that has exactly the same function as CTest above:
class CTestPOD: public ITest1POD
{
int ref;
public: inline CTestPOD() { ref = 0; }
inline void AddRef() { ++ref; }
inline void Release() {--ref; }
inline void DoIt(int x) {ref *= x; }
};
Our purpose is to use this CTestPOD to make an apples-to-apples comparison with CTest:
void CTest::TestPerf(int loop)
{
clock_t begin = clock();
for(int i = 0; i < loop; ++i) //loop1
{
CTestPOD testPOD; // line1
testPOD.AddRef();
testPOD.DoIt(0);
testPOD.Release();
}
clock_t end = clock();
printf("POD time: %f n",double(end - begin) / CLOCKS_PER_SEC);
begin = clock();
for(int i = 0; i < loop; ++i) //loop2
{
CTest test; // line2
test.AddRef2();
test.DoIt2(0);
test.Release2();
}
end = clock();
printf("Interface time: %f n",double(end - begin) / CLOCKS_PER_SEC);
}
The only difference between the above loop1 and loop2 is line1 and line2. In order to avoid using virtual functions, I specially prepared AddRef2, DoIt2, and Release2 for CTest, three of the same But it is a non-virtual function in order to follow one of the major principles of performance testing: compare apple to apple.
I set loop to 100,000, and the test results show that loop2 is about 20% slower than loop1. Judging from the generated code, the only difference is that the constructor of CTest calls the constructor of ITest1 that is automatically generated by compilation. This constructor does nothing but takes up many CPU cycles. A good compiler should be able to cut out this constructor. It is up to us to search for this ourselves.
Summary
When applying inheritance, removing useless constructors in the base class will have a significant impact on the performance of a large number of constructed objects. Unfortunately, Microsoft's __declspec(novtable) class modifier does not provide any help in solving this problem. When designing applications for mass storage objects, we should try to use POD as its base class to avoid obvious performance loopholes like the CTest class above.
2014-9-3 Seattle
In all solids, atoms are held together by bonds. Bonds give solids their strength and corresponding electrical and thermal properties. For example, strong bonds lead to high melting points, high elastic modulus, short interatomic distances, and low thermal expansion coefficients.
1. Chemical bonding
1. Ionic bond
Ionic bond is caused by the mutual attraction of positive and negative charges. For example, a sodium atom has an electron in its valence orbit, which can easily release its outer electrons and become a positively charged ion. Likewise, chlorine atoms readily accept an electron into their valence orbitals up to eight electrons and become negatively charged ions. Since there is always an electrostatic attraction between negatively and positively charged materials, bonds are formed between adjacent ions with different charges. The characteristic of ionic bonds is that adjacent to positive ions are negative ions, and adjacent to negative ions are positive ions, such as NaCl crystals, see Figure 2-1.
2 Covalent bond
Covalent bond is a strong attractive bond. When two identical atoms or atoms with similar properties are close to each other, the valence electrons will not be transferred. The atoms are combined by the force generated by the shared electron pairs to form a covalent bond. Covalent bonds create strong attractions between atoms, which is evident in diamond, which is the hardest material in nature and is composed entirely of carbon atoms. Each carbon atom has four valence electrons, which are shared with neighboring atoms to form a three-dimensional lattice composed entirely of valence electron pairs. These three-dimensional lattices give diamond its high hardness and melting point.
3. Metallic Bonds
Metals are made up of metallic bonds, which have completely different properties from non-metals. Metal atoms have few outer electrons and are easily lost. When metal atoms are close to each other, these outer atoms break away from the atoms and become free electrons, which are shared by the entire metal. The free electrons move inside the metal to form an electron gas. This combination between free electrons and metal cations is called a metallic bond, as shown in Figure 2-2. 4. Molecular bond
Molecular bond, also called van der Waals bond, is the weakest bond. It relies on the uneven distribution of electrons within the atoms to produce weak electrostatic attraction, called van der Waals force. The bonds combined by this molecular force are called molecular bonds.
5. Hydrogen Bonding
Another type of van der Waals force is actually a special case of polar molecules. The exposed proton at the end of the C-H, O-H or N-H bond is not shielded by electrons. Therefore, this positive charge can attract the valence electrons of adjacent molecules, thus forming a Coulomb-type bond called hydrogen. Bonds, hydrogen bonds are the strongest of all van der Waals bonds. The most typical example of hydrogen bonding is water. The hydrogen proton in one water molecule attracts the lone pair of electrons of oxygen in the adjacent molecule. The hydrogen bond makes water have the highest boiling point among all low molecular weight substances.
2. The influence of bonding bonds on material properties
1. Metal Materials
The bonding bonds of metal materials are mainly metal bonds. Due to the existence of free electrons, when a metal is subjected to an external electric field, the free electrons inside it will move directionally along the direction of the electric field, forming an electron flow, so the metal has good electrical conductivity; in addition to relying on the vibration of positive ions to transfer heat energy, metals also The movement of free electrons can also transfer heat energy, so metals have good thermal conductivity; as the temperature of metal increases, the thermal vibration of positive ions intensifies, which increases the resistance to the directional movement of free electrons and increases the resistance, so metals have positive resistance. Temperature coefficient; when the two parts of the metal are relatively displaced, the positive ions of the metal still maintain the metallic bond, so it has good deformation ability; the free electrons can absorb the energy of light, so the metal is opaque; and the absorbed energy returns to the In its original state, it produces radiation, giving the metal its luster.
There are also covalent bonds (such as gray tin) and ionic bonds (such as the intermetallic compound Mg3Sb2) in metals.
2. Ceramic materials
Simply put, ceramic materials are compounds containing metal and non-metal elements. Their binding bonds are mainly ionic bonds and covalent bonds, most of which are ionic bonds. Ionic bonds give ceramic materials considerable stability, so ceramic materials usually have extremely high melting points and hardness, but at the same time ceramic materials are also very brittle.
3. Polymer materials
The binding bonds of polymer materials are covalent bonds, hydrogen bonds and molecular bonds. Among them, the binding bonds that make up molecules are covalent bonds and hydrogen bonds, while the binding bonds between molecules are van der Waals bonds. Although the van der Waals bond is weak, because the molecules of polymer materials are large, the intermolecular forces are correspondingly large, which makes polymer materials have good mechanical properties.
3. Crystals and Amorphous
After studying the bonding bonds, our next task is to consider the material from the arrangement of atoms or molecules... The rest of the full text >>
Discussion and introduction of new glass curtain wall specifications and engineering inspection standards
Shandong Academy of Building Sciences (Shandong Construction Machinery Quality Supervision and Inspection Center)
Li Chengwei
Shandong Construction Machinery Quality Supervision and Inspection Center, as a full-time construction curtain wall testing unit in our province, is also a designated testing unit for national industrial product production licenses. In recent years, it has conducted long-term testing and tracking of construction curtain wall projects in our province. Through the investigation, we initially gained first-hand information on the manufacturing quality and construction level of curtain wall projects in our province. Comparing the technical level of domestic advanced enterprises, there is a certain gap between the curtain wall enterprises in our province not only in the production technology level, but also in the technical design and construction methods of building curtain walls. This is mainly reflected in the understanding of specifications and There is a big gap in implementation. Below, let’s talk about our views on the design, calculation and understanding of specifications for building curtain walls.
1. Overview of the revised content of "Technical Specifications for Glass Curtain Wall Engineering" JGJ102-2003
"Technical Specifications for Glass Curtain Wall Engineering" JGJ102-2003 has been implemented since January 1, 2004, and is the original industry standard "Technical Specifications for Glass Curtain Wall Engineering" JGJ102-96 will be abolished at the same time. The revision of JGJ102-2003 is relatively large. It not only adds mandatory provisions, but also adds the contents of full-glass curtain wall and point-supported curtain wall, and also makes major revisions to other contents.
(1) Added mandatory terms:
1. Silicone structural sealant is used in hidden frame and semi-hidden frame glass curtain walls, and the bonding between the glass and aluminum profiles must be Medium duty silicone structural sealant.
Before using silicone structural sealant, the compatibility and peel adhesion test of the materials in contact with it should be carried out by a nationally recognized testing agency, and the Shore hardness and tensile bonding under standard conditions should be tested. Performance is retested. Products that fail the inspection shall not be used. Imported silicone structural sealants should have a commodity inspection report. Silicone structural sealants and silicone building sealants must be used within the expiration date.
Silicone structural sealants should be subjected to load-bearing limit state calculations based on different stress conditions.
Except for all-glass curtain walls, silicone structural sealants should not be injected on site.
2. The bearing capacity and deflection of structural members should be checked according to regulations and meet the specification requirements.
The thickness of the opening of the aluminum profile section at the main stress-bearing part of the column should not be less than 3.0mm, and the thickness of the closed part should not be less than 2.5mm; when the profile hole wall and the screw are directly connected by threaded force, the The local thickness should not be less than the nominal diameter of the screw; the thickness of the main stress-bearing part of the steel profile section should not be less than 3.0mm.
The thickness of the aluminum alloy profile at the main stress-bearing part of the beam should not be less than 2.0mm; when the span of the beam is greater than 1.2 m, the thickness of the main stress-bearing part of the cross-section should not be less than 2.5mm. When the threaded force connection is directly used between the profile hole wall and the screw, the local section thickness should not be less than the nominal diameter of the screw. The thickness of the main stress-bearing parts of the steel profile section should not be less than 2.5mm.
3. The glass curtain walls of public places with high density of people, where teenagers or young children are active, and parts that are prone to impact during use should be made of safety glass; for parts that are prone to impact during use, there should be clearly visible warning signs.
(2) The main contents of the all-glass curtain wall:
The thickness of the panel glass should not be less than 10mm. When the panel glass is laminated glass, the thickness of the single piece should not be less than 8 mm.
The cross-sectional thickness of the glass ribs should not be less than 12mm, and the cross-sectional height should not be less than 100mm. Moreover, this provision is mandatory.
Under the standard value of wind load, the deflection limit of the glass rib should be 1/200 of its calculated span.
The thickness of curtain wall glass using floating head connectors should not be less than 6 mm; the thickness of curtain wall glass using countersunk head connectors should not be less than 8 mm. The width of the gaps between the glass should not be less than 10 mm and should be caulked with silicone building sealant. This part is mandatory.
The supporting structure of point-supported glass curtain wall should be calculated separately.
Whether a single section steel or steel pipe is used as a supporting structure, a truss or a hollow truss is used as a supporting structure, under the action of the standard value of wind load, its deflection limit should be 1/250 of its span.
The tension rod and cable system should form a stable structural system in both positive and negative directions that can withstand wind loads or earthquake effects. Connectors, compression rods or tie rods should be made of stainless steel, and the diameter of the rods should not be less than 10mm. The diameter of the cable strand should not be less than 8 mm. The tie rod should not be welded, and the cable should not be welded. Under the action of the standard value of wind load, its deflection limit should be taken as its span... The rest of the full text >>