#include <iostream>
#include <functional>
#include <chrono>
using namespace std;
void printftmp(int n)
{
if (n)
{
printftmp(n - 1);
//cout << n << " ";
}
}
void printftmp1(int n)
{
if (n)
{
for (int i = 0; i < n; ++i)
{//cout << n << " ";
}
}
}
void temp(std::function<void(int)> cb)
{
auto begintime = chrono::system_clock::now();
cb(10000);
auto endtime = chrono::system_clock::now();
auto TimeCount = std::chrono::duration<double, std::milli>(endtime - begintime).count();
cout << "所用时间:" << TimeCount << "ms." << endl;
}
int main()
{
temp(std::bind(printftmp,100000)); // 递归
temp(std::bind(printftmp1, 100000)); // for循环
return 0;
}
打印输出:
所用时间:2.09816ms.
所用时间:0.317457ms.