简体   繁体   English

C99可变长度自动阵列性能

[英]C99 variable length automatic array performance

Is there significant cpu/memory overhead associated with using automatic arrays with g++/Intel on 64-bit x86 linux platform? 在64位x86 Linux平台上使用带有g ++ / Intel的自动数组是否会产生显着的CPU /内存开销?

int function(int N) {
    double array[N];
  • overhead compared to allocating array before hand (assuming function is called multiple times) 与先前分配数组相比的开销(假设函数被多次调用)

  • overhead compared to using new 与使用新的相比开销

  • overhead compared to using malloc 与使用malloc相比的开销

The range of N may be from 1kb to 16kb roughly, stack overrun is not a problem. N的范围可以粗略地从1kb到16kb,堆栈溢出不是问题。

The difference in performance between a VLA and a statically-sized array should be negligible. VLA和静态大小的阵列之间的性能差异应该可以忽略不计。 You may need a few extra instructions to calculate how much to grow the stack but that should be noise in any real program. 您可能需要一些额外的指令来计算堆栈的增长量,但这应该是任何实际程序中的噪声。

Hmm, on further thought, there could also be some overhead depending on how the local variables are layed out in memory and whether there are multiple VLAs. 嗯,进一步考虑,根据局部变量在内存中的布局以及是否存在多个VLA,也可能存在一些开销。

Consider the case where you have the locals (and assume they are put in memory in the order they are specified). 考虑你有本地人的情况(并假设他们按照指定的顺序放入内存)。

int x;
int arr1[n];
int arr2[n];

Now, whenever you need to access arr2 , the code needs to calculate the location of arr2 relative to your base pointer. 现在,无论何时需要访问arr2 ,代码都需要计算arr2相对于基指针的位置。

  • Review the assembly output 查看装配输出
  • Profile it, for your application 为您的应用程序配置它
  • Check your memory usage 检查你的内存使用情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM