MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
做ACM題目的時(shí)候 , 經(jīng)常使用到 fill, memset , for 操作對(duì) 數(shù)據(jù)進(jìn)行初始化操作, 在測(cè)試數(shù)據(jù)不大,而且數(shù)組范圍也不大的情況下,
這幾種操作的時(shí)間差距不是很明顯. 但是!!!! 因?yàn)?span style="color: red; ">測(cè)試數(shù)據(jù)的數(shù)量有時(shí)候非常大!!因此對(duì)數(shù)據(jù)初始化 操作的 選擇也變得非常重要.
于是就對(duì)3種操作進(jìn)行了一個(gè)小測(cè)試.......................... 測(cè)試如下:
測(cè)試系統(tǒng) 及 配置:

測(cè)試方案 : 開了3億的數(shù)組, 對(duì)每個(gè)函數(shù)調(diào)用三次
測(cè)試結(jié)果 :
fill : G++ C++
memset : G++ C++
for : G++ C++

從上面的 數(shù)據(jù)對(duì)比可以看到 , 除去誤差外, 2種編譯器對(duì)數(shù)據(jù)的處理時(shí)間 基本是一致的, 對(duì)于第一次處理 , 都額外花費(fèi)了500MS 左右的時(shí)間, 因?yàn)?/p>
盡管一開始申請(qǐng)了3億的靜態(tài)數(shù)組空間, 但是系統(tǒng)并不會(huì)全部把它給你, 只是代表你有權(quán)使用它, 這就要感謝操作系統(tǒng)的 內(nèi)存管理機(jī)制了.
所以第一次
處理都花費(fèi)了額外的時(shí)間來(lái)分配內(nèi)存. 這也是為什么ACM 中很多題, 明明開了1000000 或更大一點(diǎn)的數(shù)組 但是內(nèi)存開銷卻沒超過(guò) 1000KB 的原因.
現(xiàn)在我們看后面的數(shù)據(jù), 可以看到 memset 的時(shí)間優(yōu)勢(shì)非常明顯, 是 fill 和 for 的2.5倍左右 !!!.
但是 . 我不得不說(shuō)明其局限性, 因?yàn)?nbsp;memset 是
每字節(jié)賦值的, 所以一般情況下, 僅能用與對(duì) 0 或 -1 的賦值, ( memset 在每個(gè)字節(jié)的末尾填充 設(shè)定值 ).
對(duì)于每個(gè)變量的其他確定性的賦值就顯得
力不從心了. 這時(shí)候就要用到 fill 或 for 循環(huán)賦值了, 原來(lái)一直覺得 fill 會(huì)很慢, 所以就一直沒用, 現(xiàn)在測(cè)試了一下才知道, 原來(lái)速度是基本一樣的, 現(xiàn)在做題可以
偷下懶了, 
忘記附代碼了 ....
代碼/*
MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : fill
*/
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
const int M = 300000000;
int a[M];
int main ()
{
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" );
return 0;
}
代碼
/*
MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : memset
*/
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
const int M = 300000000;
int a[M];
int main ()
{
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" );
return 0;
}
代碼/*
MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : for
*/
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
const int M = 300000000;
int a[M];
int main ()
{
time_t beg,end; int i;
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" );
return 0;
}