我们知道内存对齐是为了方便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<<sizeof(i)*8 -1);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.200001real 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<<sizeof(i)*8 -1);printf("%d\n",(int)sizeof(st1));for(i=0;i<1036854775807;i++);{//for(j=0;j<9223372036854775807;j++);//9223372036854775807x = 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
1256.200001real 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<<sizeof(i)*8 -1);printf("%d\n",(int)sizeof(st1));for(i=0;i<1036854775807;i++);{//for(j=0;j<9223372036854775807;j++);//9223372036854775807x = 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
1256.200001real 32m55.346s
user 32m55.587s
sys 0m0.008s
4、总结
real 34m22.283s ------------> 1字节对齐
real 32m55.646s ------------> 4字节对齐
real 32m55.346s ------------> 默认字节对齐
但是这样得出内存对齐让系统运行更快这个结论我觉得还是有点草率,后面我再加大测试,毕竟现在是多进程运行,有可能在进程运行的时候被抢占CPU。
大家也可以说出自己的看法。
推荐阅读:
专辑|Linux文章汇总
专辑|程序人生
专辑|C语言
我的知识小密圈