for 与while 的比较

    它跟其他的,如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;
}

发布了21 篇原创文章 · 获赞 3 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/fafactx/article/details/19915569