C 中的静态与全局在速度和空间消耗方面
我想知道静态变量和全局变量在访问速度和空间消耗方面的区别。 (如果你想了解我的平台:Windows 上的 gcc 编译器。(我使用 Cygwin 和 Triton IDE 在 Windows 上进行 ARM7 嵌入式编程。Triton 附带 Java 平台上的 gcc 编译器,可以在 Windows 上运行。))
(显然我知道就这个问题)
编辑:好的,给我一个关于任何微控制器/处理器环境的答案。
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.))
(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
空间没有区别,它们占用的空间相同。
但有一个速度差异:静态更快。
当然对于全局变量和静态变量的内存访问是一样的。 但是当你有静态时编译器可以优化。 当它编译模块时,它知道对模块外部函数的任何函数调用都不能更改静态变量。 因此它确切地知道发生了什么,并且可以通过函数调用将其保存在寄存器中。 当它是全局的并且您从不同的模块调用函数时,编译器无法知道它的作用。 因此,他必须假设函数访问变量并更改它,从而导致存储和重新加载。
使用 gcc,您可以同时传递所有
.c
源,因此它还可以查看对来自不同模块的函数的函数调用中发生的情况。 要使其工作,您必须同时传递所有.c
文件之外的-combine
和-fwhole-program
。-fwhole-program
使所有全局变量静态(不是模块静态,而是编译单元静态,即所有给定的.c
文件在一起)。-combine
进行模块间分析。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 e.g. 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. To make it work you have to pass besides all.c
files at once-combine
and-fwhole-program
. The-fwhole-program
makes all globals static (not module static but compilation unit static, i.e. all the given.c
files together). The-combine
makes the intermodule analysis.空间消耗:基本没有区别。 唯一出现空间问题的情况是,如果您设法将相同的静态数据块隐藏在 N 个对象文件中,那么您将得到 N 的乘数,如果它是单个全局片段,您可能只有 1 个副本数据的。 然而,这是一个设计错误的问题。 信息隐藏是好的——除非信息不应该被隐藏。
访问速度:无差别。
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. 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.
当涉及到空间时,您描述的环境没有差异。 静态变量或全局变量消耗相同数量的内存。
出于速度考虑(但不是好的做法),如果您需要访问一个文件之外的 var,您可以首选全局变量。
(参考使用
external char my_global_char_placed_else_where;
)为了更好的实践,您可以使用 get/set 函数,但它们速度较慢。 因此,您可以使用宏来获取/设置全局变量,以向代码读者隐藏该变量实际上是全局的,但这有点像作弊。 但它可以使代码更具可读性。
如果比较将 var 隐藏在函数内部,那么与将其放在函数外部没有什么区别,并且更多函数可以访问该 var。
我自己使用MSP430、ARM7(仅用于测试)和AVR32 micros进行开发
There is no difference in the env you describe when it comes to space. The static or global var consume just the same amount of memory.
For speed considerations (but not good practice) you could prefer global vars, if you need access to the var outside the one file.
(ref use of
external char my_global_char_placed_else_where;
)For better practice you use get/set functions instead but they are slower. 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. 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.
I myself use MSP430, ARM7(just for tests) and AVR32 micros for development
乔纳森所说的并不完全正确。 静态变量和全局变量都将(必须)保存在 ZI(或 RW 数据)区域中。 编译器不能严格地将其“保留”在寄存器上 - 它可能做的是将值加载到寄存器中,使用该寄存器进行所有操作,然后将该值保存回来 - 这是编译器特定的优化。 即便如此,编译器也没有理由不对全局变量执行此操作:除非您将其设置为易失性。 但是,从技术上讲,您也可以将静态变量设置为易失性,所以同样没有区别。
编辑:哦是的 - 空间:没有区别。
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. 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.
Edit : oh yeah - space : no difference.