C:将数据从堆复制到堆栈的成本?

时间:2022-12-09 08:58:53

I am learning C and would like to ask about best practices.

我正在学习C,并想询问最佳实践。

Here two implementations (shirt is struct):

这里有两个实现(衬衫是struct):

implementation1 :

实施1:

void printShirt(shirt shirt){
    printf("(%d , %s)",shirt.size,shirt.color);
}
void printShirts(shirt * shirts, int nbShirts){
    int i;
    for(i=0;i<nbShirts;i++) printShirt(shirts[i]);
    printf("\n");
}

implementation 2:

实施2:

void printShirt(shirt * shirt){
    printf("(%d , %s)",shirt->size,shirt->color);
}
void printShirts(shirt * shirts, int nbShirts){
    int i;
    for(i=0;i<nbShirts;i++) printShirt(&shirts[i]);
    printf("\n");
}

If I am correct (?), in implementation 1 the data of each shirt is copied from heap to stack before print. But this does not happen in implementation 2 (?).

如果我是正确的(?),在实现1中,每个衬衫的数据在打印之前从堆复制到堆栈。但这不会发生在实现2(?)中。

For big arrays and structure might this have an impact ? Is there some best practice to follow ?

对于大型阵列和结构可能会产生影响吗?是否有一些最好的做法可以遵循?

2 个解决方案

#1


4  

Yes, this can have an impact on large structures, or large arrays of smaller structures, unless the compiler is able to optimize the call away. It's far more normal to avoid copying data when you already have a copy of it - especially if you aren't intending to modify the copy.

是的,除非编译器能够优化呼叫,否则这会对大型结构或大型小型结构阵列产生影响。当您已经拥有数据副本时,避免复制数据更为正常 - 特别是如果您不打算修改副本。

Copying a struct is quite fast - it essentially just blits bytes from one location in memory to another... But copying a pointer is faster. If the struct is small, you might consider copying it so that you avoid extra pointer dereferencing. However, by now you may well be getting into premature optimization.

复制结构非常快 - 它实际上只是将字节从内存中的一个位置缓存到另一个位置......但复制指针的速度更快。如果结构很小,您可以考虑复制它,以避免额外的指针解除引用。但是,到现在为止,您可能会进入过早优化阶段。

Back to the pointer approach, normally you help the programmer to realise that the data will not be modified by receiving a const pointer:

回到指针方法,通常你会帮助程序员意识到通过接收一个const指针不会修改数据:

void printShirt( struct shirt const * shirt )

Your example is a bit naive, because the cost of printf is far greater than copying even a relatively large structure. But I understand what you're trying to ask.

你的例子有点幼稚,因为printf的成本远远大于复制相对较大的结构。但我明白你要问的是什么。

#2


-1  

You will use less memory the implementation 2 since you only pass in the address (pointer) to the printShirt function. Implementation 1 could be slow if sizeof shirt is big and it will also consumes more memory since you're going to copy all fields in the struct.

您将使用较少的内存实现2,因为您只将地址(指针)传递给printShirt函数。如果衬衫的尺寸很大,实施1可能会很慢,因为你要复制结构中的所有字段,它也会消耗更多的内存。

#1


4  

Yes, this can have an impact on large structures, or large arrays of smaller structures, unless the compiler is able to optimize the call away. It's far more normal to avoid copying data when you already have a copy of it - especially if you aren't intending to modify the copy.

是的,除非编译器能够优化呼叫,否则这会对大型结构或大型小型结构阵列产生影响。当您已经拥有数据副本时,避免复制数据更为正常 - 特别是如果您不打算修改副本。

Copying a struct is quite fast - it essentially just blits bytes from one location in memory to another... But copying a pointer is faster. If the struct is small, you might consider copying it so that you avoid extra pointer dereferencing. However, by now you may well be getting into premature optimization.

复制结构非常快 - 它实际上只是将字节从内存中的一个位置缓存到另一个位置......但复制指针的速度更快。如果结构很小,您可以考虑复制它,以避免额外的指针解除引用。但是,到现在为止,您可能会进入过早优化阶段。

Back to the pointer approach, normally you help the programmer to realise that the data will not be modified by receiving a const pointer:

回到指针方法,通常你会帮助程序员意识到通过接收一个const指针不会修改数据:

void printShirt( struct shirt const * shirt )

Your example is a bit naive, because the cost of printf is far greater than copying even a relatively large structure. But I understand what you're trying to ask.

你的例子有点幼稚,因为printf的成本远远大于复制相对较大的结构。但我明白你要问的是什么。

#2


-1  

You will use less memory the implementation 2 since you only pass in the address (pointer) to the printShirt function. Implementation 1 could be slow if sizeof shirt is big and it will also consumes more memory since you're going to copy all fields in the struct.

您将使用较少的内存实现2,因为您只将地址(指针)传递给printShirt函数。如果衬衫的尺寸很大,实施1可能会很慢,因为你要复制结构中的所有字段,它也会消耗更多的内存。