• <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>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            fill memset for 小測試

            Posted on 2010-09-02 10:47 MiYu 閱讀(1885) 評論(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
            受教了 ``
            少妇熟女久久综合网色欲| 无码久久精品国产亚洲Av影片| 久久久无码精品午夜| 伊色综合久久之综合久久| 久久久久久久97| 亚洲乱码日产精品a级毛片久久 | 精品国产乱码久久久久久人妻| 久久久久久夜精品精品免费啦| 国内精品伊人久久久久影院对白| 久久天天躁狠狠躁夜夜躁2014| 波多野结衣中文字幕久久| 国产精品成人久久久| 国产午夜福利精品久久| 热re99久久6国产精品免费| 久久一区二区三区免费| 欧美亚洲国产精品久久蜜芽| 久久午夜伦鲁片免费无码| 久久99热这里只有精品66| 久久久久亚洲爆乳少妇无| 久久精品一区二区影院| 青青青国产精品国产精品久久久久| 久久精品水蜜桃av综合天堂| 久久久久久久波多野结衣高潮 | 久久精品亚洲中文字幕无码麻豆| 久久精品国产亚洲Aⅴ香蕉 | 久久久久亚洲av成人无码电影| 免费精品99久久国产综合精品| 国产产无码乱码精品久久鸭| 久久99精品久久久久久动态图| 狼狼综合久久久久综合网| 四虎久久影院| 噜噜噜色噜噜噜久久| 一级a性色生活片久久无| 久久影视综合亚洲| 亚洲国产精品嫩草影院久久| 伊人色综合九久久天天蜜桃| 狠狠色婷婷久久一区二区| 久久综合亚洲欧美成人| 国产午夜久久影院| 久久精品国产黑森林| 亚洲国产香蕉人人爽成AV片久久 |