您的位置:首页 > 其它

for 与while 的比较

2014-02-25 15:20 337 查看
它跟其他的,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变量。这使得For循环能够知道在迭代过程中的执行顺序。----http://zh.wikipedia.org/wiki/For%E8%BF%B4%E5%9C%88(wiki).

比较结果:

#include <iostream>
#include "Sort_algorithm.h"
#include <stdlib.h>
#include <ctime>
#include <iostream>

using namespace std;
int main()
{
int L = 1000000000;
clock_t start,end;
double spent_for,spent_while;
start = clock();
for(int i = 0; i < L; i++)
{}
end = clock();
spent_for = (double) (end - start) / CLOCKS_PER_SEC;

start = clock();
while(L--)
{}
end = clock();
spent_while = (double) (end - start) / CLOCKS_PER_SEC;

cout<<"for spent seconds = "<<spent_for<<endl;
cout<<"while spent seconds = "<<spent_while<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: