码迷,mamicode.com
首页 > 其他好文 > 详细

关于循环赋值、memset与fill的效率测试

时间:2020-07-10 13:03:50      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:clock   show   --   include   ++i   初始   optimize   isp   开启   

最近一直在做图论的题目。对于初始化的效率要求比较高。正巧我也对这三个函数不是很清楚。

就写了个测试程序来测试效率

测试程序:

技术图片
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;

#define max 100000000+5
int a[max];
int main()
{
    time_t beg,end;

    cout<<"---------------------------for\n";
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 1 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 2 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 3 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 4 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 5 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 6 cost: "<<end-beg<<"MS"<<endl;

    cout<<"----------------register---for\n";
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 1 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 2 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 3 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 4 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 5 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    for(register int i=0;i<max;++i) a[i]=0;
    end=clock();
    cout<<"test 6 cost: "<<end-beg<<"MS"<<endl;

    cout<<"---------------------------fill\n";
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 1 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 2 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 3 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 4 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 5 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    fill(a,a+max,0);
    end=clock();
    cout<<"test 6 cost: "<<end-beg<<"MS"<<endl;

    cout<<"---------------------------memset\n";
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 1 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 2 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 3 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 4 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 5 cost: "<<end-beg<<"MS"<<endl;
    beg=clock();
    memset(a,0,sizeof(a));
    end=clock();
    cout<<"test 6 cost: "<<end-beg<<"MS"<<endl;
    return 0;
}
View Code

技术图片

 

 开启o2优化之后

技术图片

综上,当需要数组初始化为0,1,-1,inf时,memset最快,

其他时候,循环赋值 + O2优化最快

关于循环赋值、memset与fill的效率测试

标签:clock   show   --   include   ++i   初始   optimize   isp   开启   

原文地址:https://www.cnblogs.com/Vikyanite/p/13278645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!