c - 结构初始化性能

标签 c linux performance

我正在尝试提高我的程序的性能(在 ARC 平台上运行,使用 arc-gcc 编译。话虽如此,我并不期待特定于平台的答案)。

我想知道以下哪种方法更优,为什么。

typedef struct _MY_STRUCT
{
    int my_height;
    int my_weight;
    char my_data_buffer[1024];
}MY_STRUCT;

int some_function(MY_STRUCT *px_my_struct)
{
    /*Many operations with the structure members done here*/
    return 0;
}

void poorly_performing_function_method_1()
{
    while(1)
    {
        MY_STRUCT x_struct_instance = {0}; /*x_struct_instance is automatic variable under WHILE LOOP SCOPE*/
        x_struct_instance.my_height = rand();
        x_struct_instance.my_weight = rand();
        if(x_struct_instance.my_weight > 100)
        {
            memcpy(&(x_struct_instance.my_data_buffer),"this is just an example string, there could be some binary data here.",sizeof(x_struct_instance.my_data_buffer));
        }
        some_function(&x_struct_instance);

        /******************************************************/
        /* No need for memset as it is initialized before use.*/
        /* memset(&x_struct_instance,0,sizeof(x_struct_instance));*/
        /******************************************************/
    }
}

void poorly_performing_function_method_2()
{
    MY_STRUCT x_struct_instance = {0}; /*x_struct_instance is automatic variable under FUNCTION SCOPE*/
    while(1)
    {
        x_struct_instance.my_height = rand();
        x_struct_instance.my_weight = rand();
        if(x_struct_instance.my_weight > 100)
        {
            memcpy(&(x_struct_instance.my_data_buffer),"this is just an example string, there could be some binary data here.",sizeof(x_struct_instance.my_data_buffer));
        }
        some_function(&x_struct_instance);
        memset(&x_struct_instance,0,sizeof(x_struct_instance));
    }
}

在上面的代码中,是 poorly_performing_function_method_1() 表现更好还是 poorly_performing_function_method_2() 表现更好?为什么?

一些事情要考虑..

  • 在方法#1 中,结构内存的释放、重新分配是否会增加更多开销?
  • 在方法#1 中,在初始化期间,是否进行了任何优化?喜欢calloc (乐观内存分配和在零填充页中分配内存)?

我想澄清一下,我的问题更多是关于WHICH 方法更优化,而不是关于如何使这段代码更优化。这段代码只是一个例子。

关于让上面的代码更优化,@Skizz 已经给出了正确的答案。

最佳答案

一般来说,不做某事会比做某事更快。

在您的代码中,您正在清除一个结构,然后用数据对其进行初始化。您正在执行两次内存写入,第二次只是覆盖第一次。

试试这个:-

void function_to_try()
{
  MY_STRUCT x_struct_instance;
  while(1)
  {
    x_struct_instance.my_height = rand();
    x_struct_instance.my_weight = rand();
    x_struct_instance.my_name[0]='\0';
    if(x_struct_instance.my_weight > 100)
    {
        strlcpy(&(x_struct_instance.my_name),"Fatty",sizeof(x_struct_instance.my_name));
    }
    some_function(&x_struct_instance);
  }
}

更新

要回答这个问题,哪个更优化,我会建议方法 #1,但它可能是边际的并且取决于编译器和其他因素。我的理由是没有任何分配/解除分配正在进行,数据在堆栈上,编译器创建的函数序言将为函数分配足够大的堆栈帧,这样它就不需要调整它的大小。在任何情况下,在堆栈上分配只是移动堆栈指针,因此开销不大。

此外,memset 是一种用于设置内存的通用方法,其中可能包含额外的逻辑来处理未对齐内存等边缘情况。编译器可以比通用算法更智能地实现初始化程序(至少,人们希望如此)。

关于c - 结构初始化性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19120005/

相关文章:

c - Fortran 如何返回数组?

Linux - 删除文件 2 大数据中文件 1 中的行

linux - 为什么Linux中的 'system'函数运行不了这个shellscript?

performance - 如何避免堆碎片?

java - 从 Wrapper 到原始类型以及从原始类型到 Wrapper 的空间高效转换

c - IOHIDManagerRegisterDeviceMatchingCallback这个函数在cocoa环境下运行吗?

c - shmat() 即使我有读取权限也被拒绝

performance - 证明时间复杂度函数的效率等级

c - 代码 ATMEGA328p 中的 ADC 转换错误

LINUX 如何获取未运行的 PID-s?