C ++ Lambda Functions

Source: Internet
Author: User

The convenience of functional programming is impressive in the map of Haskell. For example, you only need to perform the + 1 operation on all elements in a list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ].
[Plain]
Map (+ 1) [1 .. 10]
You can get
 
[Plain]
=> [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
 
Interested can go to http://tryhaskell.org/Try Haskell, a very nice language.
 
C ++ also began to support functional programming in the new standard, so what does functional programming bring to us? Performance is different or concise?
 
(1) Concise. The simplest example is to discuss functional programming. The value of each element in the vector <int> container vec_int is + 1. assume that vec_int stores 1 to 10.
 
Generally, the C ++ program is written as follows (the auto keyword is used for simplicity ):
 
[Cpp]
For (auto it = vec_int.begin (); it! = Vec_int.end (); ++ it)
{* It ++ ;}
 
The method to use the new standard Lambda function is:
[Cpp]
For_each (vec_int.begin (), vec_int.end (), [] (int x) {x ++ ;});
 
If the Boost library is used, it is:
[Cpp]
BOOST_FOREACH (int x, vec_int) {x ++ ;}
 
The classic map-reduce mode of functional programming (using Haskell) should be:
[Plain]
Map (+ 1) [1 .. 10]
 
What do you think about the conciseness brought by the new standard Lambda functions? In my opinion, conciseness (in dealing with such problems)
Classical functional programming> Implementation of C ++ Boost libraries> Implementation of C ++ New Standard Lambda> original C ++ imperative Programming
 
In general, C ++ expects functional programming, although not as powerful as Haskell and LISP, but it brings us the convenience of programming. In C ++, we recommend that you try the Boost library, because BOOST_FOREACH can be used not only for containers, but also for arrays.
 
 
(2) Performance
 
For this purpose, the new C ++ standard is quite surprising. The performance of using Lambda functions is actually higher than that of manual imperative programming. For the simple example above, the simple test code is as follows:
 
[Cpp]
# Include <iostream>
# Include <string>
# Include <vector>
# Include <algorithm>
# Include <boost \ foreach. hpp>
# Include <time. h>
 
Using namespace std;
 
Int main ()
{
Vector <int> vec_int;
Vec_int.reserve (10 );
For (int I = 0; I <10; ++ I)
{Vec_int.push_back (I );}
 
/* The outer layer of each loop is in the cycle times */
Int times = 100000;
 
Clock_t start = clock ();
/*************************************** ********/
For (int I = 0; I <times; ++ I)
{
For_each (vec_int.begin (), vec_int.end (), [] (int x) {x ++ ;});
}
/*************************************** *******/
Clock_t finish = clock ();
Cout <"time used is:" <finish-start <"ms" <endl;
 
 
Start = clock ();
/*************************************** ********/
For (int I = 0; I <times; ++ I)
{
BOOST_FOREACH (int x, vec_int)
{X ++ ;}
}
/*************************************** *******/
Finish = clock ();
Cout <"time used is:" <finish-start <"ms" <endl;
 
 
Start = clock ();
/*************************************** ********/
For (int I = 0; I <times; ++ I)
{
For (auto it = vec_int.begin ();
It! = Vec_int.end (); ++ it)
{* It ++ ;}
}
/*************************************** *******/
Finish = clock ();
Cout <"time used is:" <finish-start <"ms" <endl;
 
 
System ("pause ");
 
Return 0;
}
 
The test results on my machine are as follows:
 
 
The fastest is the implementation of the new C ++ standard, the second is BOOST_FOREACH, and the slowest is the imperative method. I always think that the manual imperative method should be the fastest, but the result is the opposite. Therefore, we can try to use Lambda expressions for container iteration. Of course, Boost is still an option.

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.