????LB??????
?????????????????????????????????????ò?????????????
????1??t =1 ???LB??
????2??t=2 ?????????
????3??t=3 ??ú???
????4??t=4???std::function??????LB
void TestLambdaFun(int t)
{
using namespace std;
vector<int> v1(10?? 1);
int x = 0;
int u = 0;
if (t == 1)
{
clock_t begin = clock();
for (int i = 0; i < 100000; ++i)
{
for_each (v1.begin()??
v1.end()??
[&x?? &u](int i){ u += i+(x++); });// Line 1
}
clock_t end = clock();
auto spent = double(end – begin) / CLOCKS_PER_SEC;
printf(“spent for type ‘%d’ is %f u is %d ”?? t?? spent?? u);
}
else if (t == 2)
{
clock_t begin = clock();
for (int i = 0; i < 100000; ++i)
{
auto _First = v1.begin();
auto _Last = v1.end();
for (; _First != _Last; ++_First)
{
u = *_First+(x++);                  // Line 2
}
}
clock_t end = clock();
auto spent = double(end – begin) / CLOCKS_PER_SEC;
printf(“spent for type ‘%d’ is %f u is %d ”?? t?? spent?? u);
}
else if (t == 3)
{
clock_t begin = clock();
for (int i = 0; i < 100000; ++i)
{
auto _First = v1.begin();
auto _Last = v1.end();
for (; _First != _Last; ++_First)
{
FuncAdd(u?? x?? *_First);             // Line 3
}
}
clock_t end = clock();
auto spent = double(end – begin) / CLOCKS_PER_SEC;
printf(“spent for type ‘%d’ is %f u is %d ”?? t?? spent?? u);
}
else if (t == 4)
{
clock_t begin = clock();
std::function <void (int)> lbda;
for (int i = 0; i < 100000; ++i)
{
lbda = [&](int i){ u += i + (x++); };
for_each (v1.begin()?? v1.end()?? lbda); // Line 4
}
clock_t end = clock();
auto spent = double(end – begin) / CLOCKS_PER_SEC;
printf(“spent for type ‘%d’ is %f u is %d ”?? t?? spent?? u);
}
}
void FuncAdd(int &u?? int &x?? int i)
{
u = i+(x++);
}
??????????VC++ 2010?е????????
??????debug???£?t=2????????????t=1??t=3??t=4???????????????????????????inline??????
??????release???£????/Ob1???????inline????????inline?????
????t=1??t=2?????????????t=3??????3???????????????????FuncAdd inline??????????????????????????LB?????????????????????C++ lambda expression????????????????????syntax sugar???????????????“????”??
????t=4????????t=3??????????????????std::function????????????????/Ob1????????????????????????????? operator???????????“?????”?????á?????????????????LB???std::function???????????LB???????????
???????
????C++ 11 ??lambda expression?????LB?????????????inline expression?????????????£????????????????????????????д??????????????????????????????????????????????????????????????????????????LB??????LB?????????std::function????????????????????????????????inline??á?