#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
class Time
{
public:
void Start() //开始计时
{
start_time=clock();
}
int End() //结束计时,返回消耗的时间。以ms为单位
{
return clock()-start_time;
}
private:
clock_t start_time;
};
const int MAX = 10000000;
char a[MAX];
int main()
{
int i;
char to = '/0';
Time t;
t.Start();
clock();
memset(a,to,sizeof(a));
cout << t.End() << endl;
t.Start();
clock();
for(i=0; i<MAX; i++)
a[i] = to;
cout << t.End() << endl;
t.Start();
clock();
fill(a,a+MAX,to);
cout << t.End()<<endl;
system("pause");
return 0;
}
结论:
fill的表现中规中矩,好处是可以赋各种值。
for不稳定
memset速度领先
本文通过实验证明了C++中使用fill、for循环和memset进行数组赋值的不同时间复杂度表现,其中fill表现中规中矩,对于赋值任务整体效率较高。
6070

被折叠的 条评论
为什么被折叠?



