for循环测试cpu_测试内存对齐对运行速度的影响

我们知道内存对齐是为了方便CPU工作,但是对齐和不对齐差异有多大呢?

我自己也没有实际测试过,今天就运行个代码测试看看。

1、1字节对齐的时候

#include "stdio.h"

#pragma pack(1)
struct test  
{  
    char  x1;  
    short x2;  
    float x3;  
    char  x4;  
};  
#pragma pack()

int main()
{
    long int i,j;
    struct test st1;
    double x;

    st1.x1=1;
    st1.x2=2;
    st1.x3=4.2;
    st1.x4='1';

    printf("%ld\n",0x1UL<    printf("%d\n",(int)sizeof(st1));
    for(i=0;i<1036854775807;i++);
    {
            x = st1.x1+st1.x2+st1.x3+st1.x4;
    }
    printf("%f\n",x);
    return 0;
}

程序输出

weiqifa@bsp-ubuntu1804:~/c$ gcc zijieduiqi.c && time ./a.out
-9223372036854775808
8
56.200001

real    34m22.283s
user    34m3.727s
sys     0m0.052s
weiqifa@bsp-ubuntu1804:~/c$

2、4字节对齐的时候

4字节对齐刚好方便了CPU,理论上CPU会运行更快的。

代码

#include "stdio.h"

#pragma pack(4)
struct test  
{  
    char  x1;  
    short x2;  
    float x3;  
    char  x4;  
};  
#pragma pack()

int main()
{
    long int i,j;
    struct test st1;
    double x;

    st1.x1=1;
    st1.x2=2;
    st1.x3=4.2;
    st1.x4='1';

    printf("%ld\n",0x1UL<    printf("%d\n",(int)sizeof(st1));
    for(i=0;i<1036854775807;i++);
    {
        //for(j=0;j<9223372036854775807;j++);//9223372036854775807
            x = st1.x1+st1.x2+st1.x3+st1.x4;
    }
    printf("%f\n",x);
    return 0;
}

程序输出

weiqifa@bsp-ubuntu1804:~/c$ gcc zijieduiqi.c && time ./a.out                         -9223372036854775808
12

56.200001

real    32m55.646s
user    32m55.587s
sys     0m0.008s

3、去掉字节对齐限制,再运行试试

代码

#include "stdio.h"

//#pragma pack(4)
struct test  
{  
    char  x1;  
    short x2;  
    float x3;  
    char  x4;  
};  
//#pragma pack()

int main()
{
    long int i,j;
    struct test st1;
    double x;

    st1.x1=1;
    st1.x2=2;
    st1.x3=4.2;
    st1.x4='1';

    printf("%ld\n",0x1UL<    printf("%d\n",(int)sizeof(st1));
    for(i=0;i<1036854775807;i++);
    {
        //for(j=0;j<9223372036854775807;j++);//9223372036854775807
            x = st1.x1+st1.x2+st1.x3+st1.x4;
    }
    printf("%f\n",x);
    return 0;
}

程序输出

weiqifa@bsp-ubuntu1804:~/c$ gcc zijieduiqi.c && time ./a.out                         -9223372036854775808
12

56.200001

real    32m55.346s
user    32m55.587s
sys     0m0.008s

4、总结

real    34m22.283s   ------------> 1字节对齐
real    32m55.646s   ------------> 4字节对齐
real    32m55.346s   ------------> 默认字节对齐

但是这样得出内存对齐让系统运行更快这个结论我觉得还是有点草率,后面我再加大测试,毕竟现在是多进程运行,有可能在进程运行的时候被抢占CPU。

大家也可以说出自己的看法。

推荐阅读:专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈

340a63f0838a84f601115418be0d8539.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值