Using lambda functions in C + + to improve code performance

Source: Internet
Author: User

The key to making compilers and operating systems extract higher performance from the applications being created is to provide sufficient information about the intent of the code. With a full understanding of the functionality that this code intends to implement, it is possible to maximize the concurrent throughput of the code at compile time and at run time, so that developers can focus more on the business aspects of their concerns, and entrust the heavyweight multicore multiprocessor task plan to the compiler, The runtime library and the infrastructure code in the operating system to handle it.

Cyclic functions are a very important part, because in all the available hardware resources, parts of the separated loops can generally provide higher application performance. Consider a small case where you iterate over all the elements in the selected group to get the sum. The simplest and most straightforward way to do this is as follows:

std::vector﹤int﹥ v;
v.push_back(1);
v.push_back(5);
int total = 0;
for (int ix = 0; ix ﹤ v.size(); ++ix){
total += v[ix];
}

The above examples are very convenient for manual reading and writing. The intent of this cycle is also easy to understand for developers who are familiar with the grammar of the C language family. However, for the combination of compilers and run-time libraries, to plan the cycle between multiple threads, it also requires instructions similar to those of the OpenMP compilation indicator to tell it where there is space for optimization:

std::vector﹤int﹥ v;
v.push_back(1);
v.push_back(5);
int total = 0;
#pragma omp for
for (int ix = 0; ix ﹤ v.size(); ++ix){
#pragma omp atomic
total += v[ix];
}

The first OpenMP directive presents the requirement for multithreaded running for loops, while the second omp atomic instruction is used to prevent multithreading from simultaneously writing to the total variable. For OpenMP, there is a detailed description of all the instructions in the reference documentation for the MSDN Library.

If you use declarative looping techniques, it is much cleaner and simpler to apply a parallel approach to vector summation. The STL for_each function is an ideal alternative, and the above examples are rewritten as follows:

class Adder{
private:
int _total;
public:
Adder() : _total(0) {}
void operator ( ) ( int& i )
{
_total += i;
}
operator int ( )
{
return _total;
}
};
void VectorAdd()
{
std::vector﹤int﹥ v;
v.push_back(1);
v.push_back(5);
int total = std::for_each(v.begin(), v.end(), Adder());
}

Here, the specific for loop is discarded, and the code for the vector is cleaned up, but the solution is greatly complicated by the need to define a class with a series of running characters. Unless there is a lot of similar summation declarations in the code base, a developer will not spend much effort to define a new class just for the benefit of the STL For_each.

By carefully examining the adder class, it is clear that most of its content is intended only to satisfy the invocation conditions that use the instance as a function object. The only thing that works in this class is the line _total = i. With this in mind, C + + 0x provides a greatly simplified syntax technique that is implemented in a lambda function way. The lambda function removes the need for these Shing codes and allows the definition of a predicate function in another declaration. Thus, the Vectoradd function can be rewritten as follows:

std::vector﹤int﹥ v;
v.push_back(1);
v.push_back(5);
int total = 0;
std::for_each(v.begin(), v.end(),
[&total](int x) {total += x;}
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.