简体   繁体   English

C中速度和空间消耗方面的静态与全局

[英]Static vs global in terms of speed and space consumption in C

I would like to know difference between static variables and global variables in terms of access speed and space consumption . 我想知道静态变量和全局变量在访问速度空间消耗方面的区别。 (If you want to know my platform: gcc compiler on Windows. (I am using Cygwin with Triton IDE for ARM7 embedded programming on windows. Triton comes with gcc compiler on Java platform which can be run on Windows.)) (如果你想了解我的平台:Windows上的gcc编译器。(我在Windows上使用Cygwin和Triton IDE进行ARM7嵌入式编程.Triton在Java平台上带有gcc编译器,可以在Windows上运行。))

(Obviously I know in terms of file and function scope from this question ) (显然我从这个问题的文件和功能范围方面知道)

Edit: OK give me an answer on any micro controller / processor environment. 编辑:好的,给我一个任何微控制器/处理器环境的答案。

There is no difference for the space, they take the same amount. 空间没有区别,它们的数量相同。

But there is a speed difference: static is faster. 但速度存在差异:静态更快。

Of course the memory access to the variable is for global and static the same. 当然,对变量的内存访问是针对全局的,也是静态的。 But the compiler can optimize when you have static. 但是当你有静态时,编译器可以进行优化。 When it compiles a module it knows that no function call to a function outside the module can change a static variable. 当它编译模块时,它知道对模块外部的函数没有函数调用可以改变静态变量。 So it knows exactly what happens and can eg keep it in a register over function calls. 因此它确切地知道发生了什么,并且可以例如将其保存在函数调用的寄存器中。 When it is global and you call a function from a different module, the compiler can't know what it does. 当它是全局的并且您从另一个模块调用函数时,编译器无法知道它的作用。 Hence he must assume that the function accesses the variable and changes it, resulting in a store and reload. 因此,他必须假设函数访问变量并对其进行更改,从而导致存储和重新加载。

With gcc you can pass all .c sources at the same time, so it can then also see what happens in function calls to functions from different modules. 使用gcc,您可以同时传递所有.c源代码,因此它还可以查看函数调用不同模块中的函数时会发生什么。 To make it work you have to pass besides all .c files at once -combine and -fwhole-program . 为了使它工作,你必须立即传递所有.c文件-combine-fwhole-program The -fwhole-program makes all globals static (not module static but compilation unit static, ie all the given .c files together). -fwhole-program使所有全局变量都是静态的(不是模块静态,而是编译单元静态,即所有给定的.c文件在一起)。 The -combine makes the intermodule analysis. -combine进行模块间分析。

Space consumption: basically no difference. 空间消耗:基本没什么区别。 The only time there'd be a space issue is if you manage to get the same chunk of static data hidden in N object files, then you get a multiplication factor of N where you might have just 1 copy if it was a single global piece of data. 唯一一次出现空间问题的是,如果你设法获得隐藏在N个目标文件中的同一块静态数据,那么你得到的乘法因子为N,如果它只是一个全局的那么你可能只有一个副本数据的。 However, that's a mis-design issue. 但是,这是一个错误的设计问题。 Information hiding is good - unless the information should not be hidden. 信息隐藏很好 - 除非不应隐藏信息。

Access speed: no difference. 访问速度:没有区别。

It's hard to guess, or to estimate. 这很难猜测或估计。 It would probably take time, but I would make a sample project and test for speed. 这可能需要一些时间,但我会制作一个示例项目并测试速度。 Testing both access speed and space with a loop. 使用循环测试访问速度和空间。 Test the sample project with an emulator for that architecture. 使用该架构的仿真器测试示例项目。

I would expect any difference would come from packing (for space) and caching (for speed) issues. 我希望任何差异都来自打包(用于空间)和缓存(用于速度)问题。 Both those could also arise from just about anything else as well. 这些也可能来自其他任何事情。

There is no difference in the env you describe when it comes to space . 空间方面你所描述的env 没有区别 The static or global var consume just the same amount of memory. 静态或全局var消耗的内存量相同。

For speed considerations ( but not good practice ) you could prefer global vars , if you need access to the var outside the one file. 出于速度考虑( 但不是良好实践 ),如果需要访问一个文件外的var ,则可能更喜欢全局变量 (ref use of external char my_global_char_placed_else_where; ) (ref使用external char my_global_char_placed_else_where;

For better practice you use get/set functions instead but they are slower. 为了更好的练习,您可以使用get / set函数,但速度较慢。 So then you could use macros for get/set of a var that is global to hide from the reader of the code that the var is in fact global, but that is kind'a like cheating. 那么你可以使用宏来获取/设置一个全局的var来隐藏var实际上是全局的代码的读者,但这有点像欺骗。 But it can make the code more readable. 但它可以使代码更具可读性。

If you compare hiding a var inside a function, then it has no difference compared with placing it outside the function and more functions could have access to the var. 如果比较在函数中隐藏var,那么与将其放在函数外部相比没有区别,并且更多函数可以访问var。

I myself use MSP430, ARM7(just for tests) and AVR32 micros for development 我自己使用MSP430,ARM7(仅用于测试)和AVR32微处理器进行开发

What Jonathan says is not exactly correct. 乔纳森所说的并不完全正确。 Both static and global variables will be (has to be) saved in the ZI (or RW data) regions. 静态和全局变量都将(必须)保存在ZI(或RW数据)区域中。 The compiler cant "keep" it over the register strictly - what it might do is load the value into the register, use that register for all operations and than save that value back - thats a compiler specific optimization. 编译器不能严格地将它“保留”在寄存器上 - 它可能做的是将值加载到寄存器中,将该寄存器用于所有操作而不是保存该值 - 这是编译器特定的优化。 And even then, there is no reason why the compiler wont do that also for global variables : unless of course u make it volatile. 即使在那时,也没有理由为什么编译器也不会对全局变量这样做:除非当然你使它变得不稳定。 But then, technically you can also make a static variable volatile, so again no difference. 但是,从技术上讲,你也可以制作一个静态变量volatile,所以再没有区别。

Edit : oh yeah - space : no difference. 编辑:哦是的 - 空间:没有区别。

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

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