c - 具有单个成员的结构是否具有与成员类型相同的性能?

标签 c performance struct member

具有单个成员的 struct 是否具有与成员类型相同的性能(内存使用和速度)?

示例:

这段代码是一个只有一个成员的struct:

struct my_int
{
    int value;
};

my_int的性能和int一样吗?

最佳答案

同意@harper总体而言,但要注意以下几点:

“非结构化”数组和结构化数组之间存在典型差异。

char s1[1000];
// vs
typedef struct {
  char s2[1000];
} s_T;
s_T s3;

调用函数时...

void f1(char s[1000]);
void f2(s_T s);
void f3(s_T *s);

// Significant performance difference is not expected.
// In both, only an address is passed.
f1(s1);
f1(s3.s2);

// Significant performance difference is expected.
// In the second case, a copy of the entire structure is passed.
// This style of parameter passing is usually frowned upon.
f1(s1);
f2(s3);

// Significant performance difference is not expected.
// In both, only an address is passed.
f1(s1);
f3(&s3);

关于c - 具有单个成员的结构是否具有与成员类型相同的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877792/

相关文章:

c - 程序跳过用户输入(C 语言的大型模块化图书馆信息系统)

c++ - 对删除分配给结构数组的动态内存感到困惑

c - 当 vsnprintf 不可用时安全地格式化字符串

c - 如何在给定数组中查找重复项并在c中对它们求和

c - Doxygen 不包括 C 位域文档

java - 高效更新 DelayQueue 中的元素

sql - 在 SQLite WHERE 子句中组合大量条件

performance - 什么导致 Oracle tkprof 文件中 CPU 时间和耗时之间存在差异

c# - 在 WCF 中使用 DataContract 传递对象

c - 如何在 C 中使用 while 声明多个指针到指针?