下面为测试代码:
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
clock_t start, end;
start = clock();
for (int i = 0; i < 200000; i++)
{
for (int i = 0; i < 200000; i++);
}
end = clock();
cout << "正计数所用时间:"<<(end - start) / CLK_TCK << endl;
start = clock();
for (int i = 200000; i > 0; i--)
{
for (int i = 200000; i > 0; i--);
}
end = clock();
cout << "倒计数所用时间:"<<(end - start) / CLK_TCK << endl;
return 0;
测试结果:
得出结论,正向计数和逆向计数的效率相等。