青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

ACM___________________________

______________白白の屋
posts - 182, comments - 102, trackbacks - 0, articles - 0
<2011年5月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用鏈接

留言簿(24)

隨筆分類(332)

隨筆檔案(182)

FRIENDS

搜索

積分與排名

最新隨筆

最新評論

閱讀排行榜

評論排行榜

fill memset for 小測試

Posted on 2010-09-02 10:47 MiYu 閱讀(1908) 評論(9)  編輯 收藏 引用 所屬分類: C/C++ACM_資料

MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋    

 

做ACM題目的時候 , 經常使用到  fillmemset  for 操作對 數據進行初始化操作, 在測試數據不大,而且數組范圍也不大的情況下,

這幾種操作的時間差距不是很明顯.  但是!!!!   因為測試數據的數量有時候非常大!!因此對數據初始化 操作的 選擇也變得非常重要. 

于是就對3種操作進行了一個小測試.......................... 測試如下:

 

 測試系統 及 配置:        

 測試方案 :     開了3億的數組, 對每個函數調用三次

 

 測試結果 :

    fill  :                                  G++                                                                 C++

 

  

  memset  :                           G++                                                                C++

 

 

     for :                            G++                                                      C++

  

 

 從上面的 數據對比可以看到 ,  除去誤差外, 2種編譯器對數據的處理時間 基本是一致的, 對于第一次處理 , 都額外花費了500MS 左右的時間, 因為

 盡管一開始申請了3億的靜態數組空間, 但是系統并不會全部把它給你, 只是代表你有權使用它,  這就要感謝操作系統的 內存管理機制了.   所以第一次

處理都花費了額外的時間來分配內存.  這也是為什么ACM 中很多題, 明明開了1000000 或更大一點的數組  但是內存開銷卻沒超過 1000KB 的原因.

現在我們看后面的數據, 可以看到 memset 的時間優勢非常明顯,  是 fill 和 for 的2.5倍左右 !!!.   但是 . 我不得不說明其局限性, 因為 memset 是

每字節賦值的, 所以一般情況下, 僅能用與對 0 或 -1 的賦值,   (  memset 在每個字節的末尾填充 設定值 ). 對于每個變量的其他確定性的賦值就顯得

力不從心了.  這時候就要用到 fill 或 for 循環賦值了, 原來一直覺得 fill 會很慢, 所以就一直沒用,  現在測試了一下才知道, 原來速度是基本一樣的, 現在做題可以

偷下懶了,   

 

 

 

 忘記附代碼了 ....

代碼
/*
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
          
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原創, 轉帖請注明 : 轉載自 ______________白白の屋
          
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, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 1: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 2: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 3: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 4: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 5: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 6: ";
    cout 
<< end - beg << " MS" << endl;
    beg 
= clock();
    memset ( a, 
0sizeof (a) );memset ( a, 0sizeof (a) );memset ( a, 0sizeof (a) );
    end 
= clock();
    cout 
<< "memset operator Cost 7: ";
    cout 
<< end - beg << " MS" << endl;

    system ( 
"pause" ); 
    
return 0;
}

 

代碼
/*
MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋
          
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;
}

 

Feedback

# re: fill memset for 小測試  回復  更多評論   

2010-09-02 10:48 by 大淵獻
還插表情,夠辛苦的。

# re: fill memset for 小測試  回復  更多評論   

2010-09-02 17:21 by 糨糊
我認為單純的從代碼層次看或者比較還不是很清楚的說明什么。
也許編譯器在處理代碼的時候都自己做了很多優化工作,最終殊途同歸了?
但不保證每個編譯器都是這樣的。

反編譯到匯編層次分析,才更說服力。

# re: fill memset for 小測試  回復  更多評論   

2010-09-02 23:19 by chaogu
好像clock理解錯了吧。
end - begin 應該說的的clock tick而不是MS,要MS
應該還要除以CLK_PER_SEC(還是CLOCK_PER_SEC,忘了自己查查)。

# re: fill memset for 小測試  回復  更多評論   

2010-09-03 10:18 by MiYu
@大淵獻
呃, 沒啥, 做題累了 ........

# re: fill memset for 小測試  回復  更多評論   

2010-09-03 10:19 by MiYu
@糨糊
表示匯編還沒學過.... T.T 路還好長啊....

# re: fill memset for 小測試  回復  更多評論   

2010-09-03 10:23 by MiYu
@chaogu
因為 用的編譯器 的CLK_TCK宏, 本身就已經是1000, 所以沒有除了, 因為除的話得到的就是 S 了

# re: fill memset for 小測試  回復  更多評論   

2010-09-03 23:48 by flyinghearts

可以直接用 MSVC的 __stosd
VC CRT的memset實現就是使用指令: rep stosd
在一些CPU上該指令要比直接用 mov + 循環控制 要快(對現在主流的CPU這也許是是最快的內存清0方法,如果不使用SSE指令的話)。


另外,for循環好像也可以優化,使用 rep stosd指令。

# re: fill memset for 小測試  回復  更多評論   

2010-09-10 22:30 by MiYu
受教了 ``
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产精品免费| 久久先锋资源| 国产日韩久久| 国产日产精品一区二区三区四区的观看方式 | 宅男噜噜噜66一区二区66| 亚洲精品欧美在线| 一区二区三区视频观看| 亚洲综合视频一区| 久久久亚洲国产美女国产盗摄| 久久久久久亚洲综合影院红桃| 久久视频一区| 欧美视频一二三区| 国产视频一区二区在线观看| 国产色产综合产在线视频| 樱桃视频在线观看一区| 一区二区三区四区五区精品视频| 午夜一区二区三视频在线观看 | 欧美日韩国产精品一区| 欧美三级日韩三级国产三级| 国产精品视频专区| 亚洲国产精品一区在线观看不卡| 一本久久知道综合久久| 亚洲专区欧美专区| 亚洲二区在线视频| 亚洲国产成人精品久久久国产成人一区 | 久久国产精品99国产精| 免费观看国产成人| 国产精品手机在线| 日韩视频不卡中文| 久久精品国产综合精品| 亚洲精品久久久久久下一站| 欧美亚洲免费高清在线观看| 欧美日韩的一区二区| 激情久久五月天| 亚洲女ⅴideoshd黑人| 亚洲第一视频| 久久久久国色av免费看影院| 国产精品日韩欧美综合| 一级成人国产| 亚洲国产精品va在线看黑人| 欧美一区午夜精品| 国产精品女主播| 国产精品99久久久久久久vr| 免费日韩视频| 久久大综合网| 国产日韩欧美高清| 亚洲综合久久久久| 日韩视频一区| 欧美日韩大片| 亚洲视频一区二区| 最新中文字幕亚洲| 欧美大成色www永久网站婷| 在线播放视频一区| 男人的天堂亚洲| 久久久精品日韩| 海角社区69精品视频| 久久精品亚洲精品| 欧美一区二区私人影院日本 | 亚洲精品乱码| 欧美18av| 欧美v日韩v国产v| 亚洲经典在线| 亚洲国产欧美久久| 欧美精品国产| 亚洲视频在线观看免费| 99精品国产福利在线观看免费| 欧美激情一区二区在线 | 母乳一区在线观看| 亚洲国产精品综合| 亚洲黄色免费网站| 欧美日韩国产专区| 欧美一区二区三区四区视频| 亚洲免费视频中文字幕| 国产裸体写真av一区二区| 久久xxxx| 美女图片一区二区| 欧美在线一二三| 欧美国产日本高清在线| 亚洲三级国产| avtt综合网| 国产综合欧美在线看| 欧美成人精品在线| 欧美日韩国产不卡| 欧美中文字幕不卡| 麻豆成人91精品二区三区| 亚洲精品资源| 亚洲一区二区在| 一区免费观看| 一本色道**综合亚洲精品蜜桃冫| 国产精品美女久久久久久免费| 久久夜色精品国产| 欧美日韩不卡一区| 久久久久久久综合狠狠综合| 蜜臀av国产精品久久久久| 亚洲一区二区三区视频| 久久久国产精品一区二区中文| 99视频一区二区| 久久国产乱子精品免费女| av成人免费| 久久网站热最新地址| 午夜国产精品视频| 欧美电影免费观看| 久久三级福利| 国产精品乱码一区二三区小蝌蚪| 欧美成人免费网站| 国产嫩草一区二区三区在线观看| 亚洲电影免费观看高清完整版在线观看| 欧美视频在线观看免费| 欧美激情精品久久久久久变态| 国产精品稀缺呦系列在线| 91久久久一线二线三线品牌| 国产一级一区二区| 一区二区三区四区蜜桃| 亚洲欧洲另类| 午夜久久久久| 亚洲欧美日韩区| 欧美欧美在线| 亚洲国产精品久久精品怡红院| 国产在线不卡| 亚洲欧美日韩中文在线制服| 在线性视频日韩欧美| 欧美成人午夜剧场免费观看| 久久综合九色综合久99| 国产午夜精品久久久久久久| 亚洲综合成人在线| 国产精品v日韩精品v欧美精品网站| 免费欧美视频| 在线观看成人一级片| 欧美在线播放视频| 久久成人一区二区| 国产伦精品一区二区三区免费迷| 亚洲毛片视频| 中国亚洲黄色| 欧美日韩视频一区二区| 亚洲激情校园春色| 亚洲毛片av在线| 欧美交受高潮1| 99re6热在线精品视频播放速度| 日韩视频永久免费观看| 欧美国产亚洲精品久久久8v| 亚洲国产精品嫩草影院| 亚洲精品影院在线观看| 久久男人av资源网站| 久久国产精品网站| 欧美aⅴ一区二区三区视频| 永久久久久久| 免费不卡中文字幕视频| 欧美激情乱人伦| 亚洲精品中文在线| 欧美色区777第一页| 国产精品影音先锋| 久久av资源网| 欧美大片在线影院| 亚洲精品日韩一| 欧美天堂在线观看| 亚洲欧美国产va在线影院| 久久精品午夜| 99国产精品视频免费观看| 欧美午夜寂寞影院| 欧美一区2区视频在线观看| 麻豆久久精品| 亚洲乱码国产乱码精品精| 欧美视频手机在线| 久久激情一区| 亚洲精品国精品久久99热| 在线中文字幕不卡| 国产午夜精品理论片a级探花 | 欧美一级一区| 亚洲大胆av| 欧美婷婷久久| 蜜桃av噜噜一区| 亚洲一区二区三区精品在线| 浪潮色综合久久天堂| 一区二区三区欧美在线观看| 国产区精品视频| 欧美精品一区在线观看| 欧美在线欧美在线| 99精品免费视频| 美女精品网站| 亚洲欧美www| 亚洲人成77777在线观看网| 国产精品美女久久福利网站| 久久久久免费| 亚洲欧美一区二区三区久久 | 久久久999成人| 亚洲免费大片| 久久久久一区二区三区四区| 一区二区三区国产精品| 影音先锋久久精品| 国产农村妇女毛片精品久久莱园子| 欧美激情一区二区三级高清视频| 欧美在线网站| 亚洲男人的天堂在线| 日韩午夜高潮| 91久久久久久| 91久久中文| 亚洲盗摄视频| 欧美成ee人免费视频| 久久久久天天天天| 久久亚洲一区二区|