可以__attribute __((包装))影响程序的表现呢? [英] Can __attribute__((packed)) affect the performance of a program?

查看:187
本文介绍了可以__attribute __((包装))影响程序的表现呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,称为日志中有13个字符。做的sizeof(日志)后,我看到的大小不是13而是16我可以使用__attribute __((包装))得到它的13实际大小,但我不知道这是否会影响程序的性能。这是经常用到的结构。

I have a structure called log that has 13 chars in it. after doing a sizeof(log) I see that the size is not 13 but 16. I can use the __attribute__((packed)) to get it to the actual size of 13 but I wonder if this will affect the performance of the program. It is a structure that is used quite frequently.

我想是能够读取结构(13不16)的大小。我可以用宏,但如果这种结构发生改变时添加或删除即场,我想是因为我觉得这是很容易出错将不改变宏更新了新的大小。有什么建议吗?

I would like to be able to read the size of the structure (13 not 16). I could use a macro, but if this structure is ever changed ie fields added or removed, I would like the new size to be updated without changing a macro because I think this is error prone. Have any suggestion?

推荐答案

是的,它会影响到程序的性能。添加填补装置的编译器可以使用整数加​​载指令从内存中读取的东西。如果没有填充,编译器必须分装的东西,做到位转移,以获得全部价值。 (即使它是86,这是由硬件完成的,它仍必须被完成)。

Yes, it will affect the performance of the program. Adding the padding means the compiler can use integer load instructions to read things from memory. Without the padding, the compiler must load things separately and do bit shifting to get the entire value. (Even if it's x86 and this is done by the hardware, it still has to be done).

考虑一下:为什么会编译器插入随机的,未使用的空间,如果不是因为性能原因

Consider this: Why would compilers insert random, unused space if it was not for performance reasons?

这篇关于可以__attribute __((包装))影响程序的表现呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆