Performance about C++ code

1,函数声明中的类参数使用引用传递而非值传递,避免拷贝构造函数的调用。
2,The variable should be declared when it will be used.
3,declaration and construction at the same time.
  someclass obj;  //default construction
  obj = xyz; //operater =

  should be somclass obj=xyz; // copy construction only
4,using initializing list is better than construction function.
  Avoiding the allocation from heap???
  someclass::someclass(int x)
  {
     foo = x;
   }
 
  someclass::someclass(int x):foo(x)

5,"+=" is better than " + "
  foo1 = foo1 + foo2;  // a temp variable would be used
  foo1 += foo2; //no temp variable

6, ++x is better than x++
   The temp variable would be used in x++

7,be careful to use the inline function
  If the inline function is very large, the compiler would refuse inlining. and would produce a independent local function in every cpp file who include the inline function.

8,How to reduce the size of memory being used
  1) be careful to use the order of byte.
  2)If it is possible, union is a good choice when  only one member would be used in a struct.

9,How to improve the speed
  1) using the register variable
    void f()
    {
       int *p = new int[3000];
       register int *p2=p;
       for ( register int j=0; j<3000; j++)
         *p2++=0;
       delete []p;
    }
   2) using const to the object never changed
   3) compiler close the supporting to RTTI and exception.
      Because compiler would insert specific code for these functions.

10, Performance improvement should focused on the released version instead debug version.
    Debuging and optimization are differnt target
    }

### C++ Standard Library Utilities Functions The C++ Standard Library includes a variety of utility functions and tools designed to simplify programming tasks by providing reusable code components. These utilities are part of the `<utility>` header file, which contains definitions for general-purpose templates that provide support for pairs, tuples, move semantics, type traits, and more. #### Common Utility Components 1. **std::pair**: This template class represents a pair of values as one object. It allows storing two heterogeneous objects within a single unit. ```cpp std::pair<int, double> myPair(10, 3.14); int firstValue = myPair.first; // Accessing the first element double secondValue = myPair.second; // Accessing the second element ``` 2. **std::tuple**: A tuple is an extension of `std::pair` allowing multiple elements with different types stored together in a single structure[^1]. ```cpp std::tuple<int, double, std::string> myTuple(42, 3.14, "hello"); int value1 = std::get<0>(myTuple); // Retrieve the integer component double value2 = std::get<1>(myTuple); // Retrieve the double component std::string value3 = std::get<2>(myTuple); // Retrieve the string component ``` 3. **Move Semantics (`std::move`, `std::forward`)**: Introduced in C++11, these utilities enable efficient transfer of resources between objects without unnecessary copying operations[^2]. They play a critical role in optimizing performance when dealing with large data structures such as strings or vectors. ```cpp std::vector<int> sourceVector{1, 2, 3}; std::vector<int> destinationVector = std::move(sourceVector); // Transfers ownership instead of copying ``` 4. **Type Traits**: Type traits allow querying properties about types at compile time, enabling conditional compilation based on those queries. Examples include checking whether a given type is integral, floating-point, pointer, etc. ```cpp static_assert(std::is_integral_v<int>, "int must be an integral type."); ``` 5. **Swap Functionality**: Provides mechanisms for swapping contents efficiently across various containers like arrays, lists, maps, sets, among others. ```cpp std::list<int> myList1 {1, 2}, myList2 {3, 4}; myList1.swap(myList2); // Swaps content directly rather than reassigning each item individually ``` 6. **Exchange Operation**: Allows replacing existing variable's value while returning previous state simultaneously using function named exchange()[^1]. ```cpp int originalVal; int newValue = std::exchange(originalVal, 789); // Sets 'originalVal' to 789 & returns old value before assignment took place ``` These functionalities form just some examples illustrating how versatile yet powerful modern-day implementations provided through updated standards continue enhancing developer productivity significantly over traditional approaches available earlier versions only limited capabilities compared against current offerings today!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值