静态内存分配VS用C动态内存分配的成本 [英] Cost of static memory allocation vs dynamic memory allocation in C

查看:188
本文介绍了静态内存分配VS用C动态内存分配的成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很有兴趣知道什么是内存分配静态VS动态的preferred方法是良好的性能(例如,运行时间),当你知道了在 C Linux的对象/项目确切的数字。成本要小数量的对象(少量的存储器)和以及用于大量对象的(存储器巨大量)。

I am very interested to know what is the preferred method of memory allocation static vs dynamic is good for performance (e.g., running time) when you know the exact number of objects/items in C on Linux. Cost for a small number of objects (small amount of memory) and as well as for a large number of objects (huge amount of memory).

例如,A型[N] VS 键入* A =的malloc(sizeof的(类型)* N)

请让我知道。谢谢你。

请注意:我们这个基准,可能知道答案。不过,我想知道,解释这两个分配方法之间的性能差异的概念。

Note: We can benchmark this and probably know the answer. But I would like to know the concepts that explain the performance differences between these two allocation methods.

推荐答案

静态分配会快很多。静态分配可以在全球范围内发生,并且在堆栈中。

Static allocation will be much faster. Static allocation can happen at global scope, and on the stack.

在全球范围内静态分配的存储器内置于二进制图像。即所需的存储器的总大小,并且它是设在运行二进制在编译时计算的。然后,当程序加载操作系统加载器将分配足够的内存为所有的全局静态数组。我是pretty确保它发生在固定时间内对所有的分配。 (例如更多的分配不会花费更多的时间)

In global scope statically allocated memory is built into the binary image. That is the total size of the memory required, and where it is to be located in the running binary is computed at compile time. Then when the program loads the operating system loader will allocate enough memory for all the global static arrays. I'm pretty sure it happens in constant time for all the allocations. ( e.g. more allocations don't cost more time )

在本地范围,静态分配是分配在栈上。这涉及到简单地保留在堆栈上固定字节数,并在每个分配恒定时间发生。栈空间是非常有限的。

In local scope, static allocations are allocated on the stack. This involves simply reserving a fixed number of bytes on the stack, and happens in constant time per allocation. Stack space is very limited.

动态内存必须从堆上分配,而且即使在最好的情况下最分配将采取鳞比,每个分配线性的,像的n log n倍或更多的东西的时间。

Dynamic memory must be allocated from a heap, and even in the best case most allocations will take time that scales more than linear with each allocation, like n log n time or something.

实际上也讲了动态分配会比静态分配要慢许多倍。

Also practically speaking the dynamic allocation will be many times slower than static allocation.

@Update:正如在下面的评论所指出的:堆栈分配在技术上并非静态的分配(但它们在OP的提问中使用的语法形式分配)。

@update: As has been pointed out in comments below: stack allocations are not technically static allocations ( but they are allocations with the syntactic form used in OP's question ).

在谈到分配时间的时候还有,我在考虑总时间来管理内存(alloc和free)。

Also when speaking about "allocation time", I'm considering total time to manage the memory ( alloc and free ).

在一些动态分配的分配器时间比释放时间快。

In some dynamic allocators allocation time is faster than freeing time.

这也是事实,一些快速的分配器换取配置高速内存效率。在这种情况下,静态仍处于静态和堆栈allocs更好是没有时间和持续时间分别同时分配确切大小的块。

It is also true that some fast allocators trade memory efficiency for allocation speed. In these cases static is still "better" in that static and stack allocs are no time and constant time respectively while allocating exact sized blocks.

动态分配器要快权衡显著内存效率(例如哥们分配器圆长达两个大小的块的下一个动力,像33K页头将使用64K)

Dynamic allocators to be fast trade off significant memory efficiency ( e.g. buddy allocators round up to the next power of two sized block, like 33k alloc will use 64k )

这篇关于静态内存分配VS用C动态内存分配的成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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