谁来解答下!!memset,for,while,fill()初始化数组的效率对比...

来源:互联网 发布:iphone4s屏幕解锁软件 编辑:程序博客网 时间:2024/05/17 00:17

         我的多次结果还是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