誰來解答下!!memset,for,while,fill()初始化數組的效率對比…

來源:互聯網
上載者:User

         我的多次結果還是memset最快,但網上有說for()迴圈初始化數組比memset快的(在int下),因為memset每次只能操作一個位元組。不知道大家測出來結果如何或這是我的測試程式原理錯了?

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <ctime>
  4. using namespace std;
  5. const int n=10000000;
  6. template<class T>
  7. class TEST{
  8. private:
  9.     long out,a1,b1,c1,d1;
  10.     T *a,*b,*c,*d;
  11.     void test_unity(const T &x,const T &y){//memset的賦值有點特殊,所以單獨設定變數x
  12.         clock_t start, finish;
  13.         //a.for memset
  14.         start = clock();
  15.         memset(a,x,sizeof(T)*n);
  16.         finish = clock();
  17.         out=finish-start;   a1+=out;
  18.         //cout<<"memset"<<endl;
  19.         
  20.         //b.for for()
  21.         start = clock();
  22.         for(int i=0;i<n;i++)   b[i]= y ;
  23.         finish = clock();
  24.         out=finish-start;   b1+=out;
  25.         //cout<<"for"<<endl;
  26.         
  27.         //c.for while()
  28.         start = clock();
  29.         int i=0;
  30.         while(i<n)  c[i++] = y;
  31.         finish = clock();
  32.         out=finish-start;   c1+=out;
  33.         //cout<<"while"<<endl;
  34.         //d.for fill()
  35.         start = clock();
  36.         fill(d,d+n, y );
  37.         finish = clock();
  38.         out=finish-start;   d1+=out;
  39.         //cout<<"fill"<<endl;
  40.     }
  41. public:
  42.     TEST(T x1,T y1,T x2,T y2,T x3,T y3){
  43.         a1=b1=c1=d1=0;
  44.         a=new T[n];
  45.         b=new T[n];
  46.         c=new T[n];
  47.         d=new T[n];
  48.         for(int i=0;i<5;i++){
  49.             test_unity(x1,y1);
  50.             test_unity(x2,y2);
  51.             test_unity(x3,y3);
  52.         }
  53.         cout<<"memset() :"<<a1<<endl
  54.             <<"for() :"<<b1<<endl
  55.             <<"while() :"<<c1<<endl
  56.             <<"fill() :"<<d1<<endl<<endl;
  57.     }
  58.     virtual ~TEST(){
  59.         delete []a;
  60.         delete []b;
  61.         delete []c;
  62.         delete []d;
  63.     }
  64. };
  65. int main() {
  66.     cout<<"int:"<<endl;
  67.     TEST<int> test1(-1,-1,0,0,5,84215045);
  68.     //test1.~TEST();
  69.     cout<<"short:"<<endl;
  70.     TEST<short> test2(-1,-1,0,0,5,1285);
  71.     //test2.~TEST();
  72.     cout<<"long:"<<endl;
  73.     TEST<long> test3(-1,-1,0,0,5,84215045);
  74.     //test3.~TEST();
  75.     cout<<"char:"<<endl;
  76.     TEST<char> test4(0,0,'a','a','9','9');
  77.     //test4.~TEST();
  78.     cout<<"bool:"<<endl;
  79.     TEST<bool> test5(0,0,true,true,false,false);
  80.     //test5.~TEST();
  81.     cout<<"long long:"<<endl;
  82.     TEST<long long> test6(-1LL,-1LL,0LL,0LL,5LL,361700864190383365LL);
  83.     //test5.~TEST();
  84.     return 0;

 

        最終結果,memset最快,測試的資料類型的位元組越小越有優勢,foe與while相差無幾,fill最慢,看了下SGI下的fill()的原始碼,採用的是for(;first!=last;first++) *first=val;的方式,然後針對char型的是用的memset,我的理解是因為memset對多位元組的資料類型不能賦成特定的值,所以才採用的for(),也間接說明了memset比for應該要快吧。。

       以上僅是個人猜測,不對的地方還望指出,謝謝,發郵件也行,liliflashfly@gmail.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.