Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the efficiency of various operations of C++

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand the efficiency of various operations of C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Classes and structures

Nowadays, object-oriented programming is popular, and I also think that this is a way to make the code clearer and modular. The object-oriented programming style has obvious advantages and disadvantages, and its advantages are:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

If the variables are members of the same structure or class, the variables used together are also stored together, so the data cache is more efficient.

Class member variables do not need to be passed to class member functions as parameters, saving the overhead of passing parameters.

The disadvantages are:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Some programmers divide the code into too many subclasses, which is unnecessary and inefficient.

Non-static member functions have a this pointer, which is passed to the function as an implicit argument, which results in some overhead, especially in 32-bit systems, where registers are scarce resources and this pointers occupy a register.

The efficiency of virtual function is low.

In fact, the overhead of classes and member functions is not very large, if the object-oriented style can make the program structure clearer, as long as we avoid using too many function calls in the most critical part of the program, don't worry about it.

Data members of the class

When you create an instance of a class or structure, its data members are stored sequentially in the order in which they are declared. Most compilers do memory alignment of structures, which can create holes in unused bytes in structures or classes with mixed member sizes.

Struct S1 {short int a; / / 2 bytes / / 6 holes double b; / / 8 int d; / / 4 / / 4 holes}; S1 ArrayOfStructures [100]

Here, there are six unused bytes between an and b, because b must start with an address that is divisible by 8. Finally, there are four unused bytes. The reason for this is that the next instance of S1 in the array must start with an address divisible by 8 in order to align its b members with 8. By putting the smallest member last, the number of unused bytes can be reduced to 2:

Struct S1 {double b; / / 8 int d; / / 4 short int a; / / 2 / / 2 holes}; S1 ArrayOfStructures [100]

This reordering reduces the structure by 8 bytes and the entire array by 800 bytes.

Structure objects and class objects can usually become smaller by reordering data members. If you are not sure how big a structure or each of its members is, you can use sizeof, whose return value includes any unused bytes at the end of the object.

If the offset of the member from the structure or the beginning of the class is less than 128, the code that accesses the data member is more compact because the offset can be represented as an 8-digit signed number. If the offset from the beginning of the structure or class is 128 bytes or greater, the offset must be represented as a 32-bit number (the instruction set has no offset between 8 and 32 bits). For example:

Struct S2 {int a [100]; / 400 int b; / / 4 int ReadB () {return b;}}

The offset of b is 400. Any code that accesses b through a pointer or member function, such as ReadB, needs to encode the offset as a 32-bit number. If an and b are exchanged, both can be accessed by an offset encoded as an 8-bit signed number, or there is no offset at all. This makes the code more compact in order to use Cache more efficiently. Therefore, it is recommended that large arrays and other large objects come last in the structure or class declaration, and the most commonly used data members come first. If you cannot include all data members in the first 128 bytes, place the most commonly used members in the first 128 bytes.

Member functions of a class

Each time a new object of a class is declared or created, a new instance of the data member is generated. But no matter how many instances of the class, there is only one member function. Calling a member function is as fast as calling a simple function using a structure pointer or reference.

Struct S3 {int a; int b; int Sum1 () {return a + b;}}; int Sum2 (S3* p) {return p-> a + p-> b;} int Sum3 (S3 & r) {return r.a + r.b;}

These three functions, Sum1, Sum2 and Sum3, do exactly the same thing, and their efficiency is the same. Some compilers generate exactly the same code for these three functions. Sum1 has an implicit this pointer that does the same thing as p and r in Sum2 and Sum3. Whether to make a function a member of a class or to give it a pointer or reference to a class or structure is just a matter of programming style. Some compilers make Sum1 more efficient than Sum2 and Sum3 in 32-bit Windows by transferring this pointers in registers.

Static member functions cannot access any non-static data members or non-static member functions. Static member functions are faster than non-static member functions because they do not require this pointers. If member functions, it does not require any non-static member access, you can speed up by setting them as static functions.

Virtual function

Virtual functions are used to realize run-time polymorphism, which is one of the main reasons for the low efficiency of object-oriented programming compared with non-object-oriented programming. If you can avoid the use of virtual functions, it can improve the efficiency of the program. In general, you can consider using compile-time polymorphism instead of run-time polymorphism. About why virtual functions lead to program inefficiency, you can see my previous article: "

RTTI

RTTI, runtime type recognition, adds additional information to all class objects, so it is inefficient, so consider turning off the RTTI option to improve program efficiency.

Inherit

Derived class objects are implemented in the same way as simple class objects that contain members of parent and subclasses. Members of the parent and subclasses access at the same speed. In general, we can assume that using inheritance will cause almost no loss to performance.

In these cases, however, there is a slight decline in efficiency:

The subclass includes all the data members of the parent class, even if the subclass does not require the data members of the parent class.

The size of the parent data member is added to the offset of the subclass member. The code to access data members with a total offset greater than 127 bytes is slightly less compact.

Inheriting more than one parent class may cause the pointer to the base class to access objects of the derived class more complex. We can avoid multiple inheritance by creating objects in derived classes, that is, composite alternative inheritance:

Class B1; class B2; class D: public B1, public B2 {public: int c;}

Put it like this:

Class B1; class B2; class D: public B1 {public: B2 b2; int c;}

In general, inheritance should be used only if inheritance is beneficial to the logical structure of the program.

Bit domain

Bits can make data more compact. The access bit member is less efficient than the ordinary member of the access structure. If large arrays can save code space, it's okay to sacrifice a little bit of efficiency.

Use

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report