fill memset for 小测试

MiYu原创, 转帖请注明 : 转载自 __白白の屋 fill memset for 小测试

做ACM题目的时候 , 经常使用到fill,memset,for操作对 数据进行初始化操作, 在测试数据不大,而且数组范围也不大的情况下,

这几种操作的时间差距不是很明显.但是!!!!因为测试数据的数量有时候非常大!!因此对数据初始化 操作的 选择也变得非常重要.

于是就对3种操作进行了一个小测试.......................... 测试如下:

测试系统 及 配置:fill memset for 小测试 fill memset for 小测试

测试方案 :开了3亿的数组, 对每个函数调用三次

测试结果 :

fill :G++C++

fill memset for 小测试

memset :G++C++

fill memset for 小测试

for :G++C++

fill memset for 小测试

从上面的 数据对比可以看到 , 除去误差外, 2种编译器对数据的处理时间 基本是一致的, 对于第一次处理 , 都额外花费了500MS左右的时间, 因为

尽管一开始申请了3亿的静态数组空间, 但是系统并不会全部把它给你, 只是代表你有权使用它, 这就要感谢操作系统的 内存管理机制了. fill memset for 小测试 所以第一次

处理都花费了额外的时间来分配内存. 这也是为什么ACM 中很多题, 明明开了1000000 或更大一点的数组 但是内存开销却没超过 1000KB 的原因.fill memset for 小测试

现在我们看后面的数据, 可以看到memset的时间优势非常明显, 是fillfor的2.5倍左右 !!!. fill memset for 小测试 但是 . 我不得不说明其局限性, 因为memset 是

每字节赋值的, 所以一般情况下, 仅能用与对0-1的赋值, (memset 在每个字节的末尾填充 设定值). fill memset for 小测试对于每个变量的其他确定性的赋值就显得

力不从心了. 这时候就要用到fillfor循环赋值了, 原来一直觉得fill会很慢, 所以就一直没用, 现在测试了一下才知道, 原来速度是基本一样的, 现在做题可以

偷下懒了, fill memset for 小测试

忘记附代码了 ....fill memset for 小测试
fill memset for 小测试fill memset for 小测试代码/

MiYu原创, 转帖请注明 : 转载自 __白白の屋

http://www.cnblog.com/MiYu

Author By : MiYu

Test : 1

Program : fill

/



#include
<iostream>

#include
<algorithm>

#include
<ctime>

usingnamespacestd;

constintM=300000000;

inta[M];

intmain ()

{

time_t beg
=clock();

fill ( a,a
+M,0); fill ( a,a+M,0xff); fill ( a,a+M,0);

time_t end
=clock();

cout
<<"fill operator Cost 1:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 2:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 3:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 4:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 5:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 6:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

fill ( a,a
+M,0);fill ( a,a+M,0xff);fill ( a,a+M,0);

end
=clock();

cout
<<"fill operator Cost 7:";

cout
<<end-beg<<"MS"<<endl;

system (
"pause");

return0;

}

fill memset for 小测试代码
/

MiYu原创, 转帖请注明 : 转载自 __白白の屋

http://www.cnblog.com/MiYu

Author By : MiYu

Test : 1

Program : memset

/



#include
<iostream>

#include
<algorithm>

#include
<ctime>

usingnamespacestd;

constintM=300000000;

inta[M];

intmain ()

{

time_t beg,end;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 1:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 2:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 3:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 4:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 5:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 6:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

memset ( a,
0,sizeof(a) );memset ( a,0,sizeof(a) );memset ( a,0,sizeof(a) );

end
=clock();

cout
<<"memset operator Cost 7:";

cout
<<end-beg<<"MS"<<endl;



system (
"pause");

return0;

}

fill memset for 小测试fill memset for 小测试代码/

MiYu原创, 转帖请注明 : 转载自 __白白の屋

http://www.cnblog.com/MiYu

Author By : MiYu

Test : 1

Program : for

/



#include
<iostream>

#include
<algorithm>

#include
<ctime>

usingnamespacestd;

constintM=300000000;

inta[M];

intmain ()

{

time_t beg,end;
inti;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 1:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 2:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 3:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 4:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 5:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 6:";

cout
<<end-beg<<"MS"<<endl;

beg
=clock();

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

for( i=0; i!=M;++i ) a[i]=0;

end
=clock();

cout
<<"for operator Cost 7:";

cout
<<end-beg<<"MS"<<endl;

system (
"pause");

return0;

}

原文链接: https://www.cnblogs.com/MiYu/archive/2010/09/02/1815748.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/14619

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午2:19
下一篇 2023年2月7日 下午2:20

相关推荐