是否使用全局变量增加或减少的表现,编为ARM7 C code吗? [英] Does using global variables increase or decrease performance, in C code compiled for ARM7?

查看:141
本文介绍了是否使用全局变量增加或减少的表现,编为ARM7 C code吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否使用了大量全局变量的使用C code降低或提高性能,对于一个ARM7嵌入式平台进行编译时?

Does using lots of global variables in C code decrease or increase performance, when compiled for an ARM7 embedded platform?

在code基包括其中提到使用extern关键字彼此的全局变量多的C源$ C ​​$ C文件。从不同的源$ C ​​$ C文件不同的功能指向不同的全局变量。一些变量是数组。

The code base consists of multiple C source code files which refer each other's global variables using the extern keyword. Different functions from different source code files refer to different global variables. Some of the variables are arrays.

我使用的编译器是IAR的ARM EW Kickstart版本(32KB)。

The compiler I'm using is IAR's EW ARM kickstart edition (32kb).

推荐答案

这会一直降低性能并增加程序的大小与静态变量。你的问题没有具体问什么你比较。我可以看到各种替代方案,

This will always decrease performance and increase program size versus static variables. Your question doesn't specifically ask what you are comparing to. I can see various alternatives,


  1. 与静态变量。

  2. 对战按值传递的参数。

  3. 对战中传递的数组或结构的指针的值。

的<一个href=\"http://community.arm.com/groups/processors/blog/2010/07/27/how-to-load-constants-in-assembly-for-arm-architecture\"相对=nofollow> ARM博客给出了如何的加载常数的一个ARM寄存器细节。此步骤必须始终进行以获得一个全局变量的地址。编译器将不知道以前的一个全球性的有多远。如果你使用的的gcc 的有 -lto 或使用类似的整个程序的,那么更好的优化可以执行。基本上,这些将改变的全球的到的静态

The ARM blog gives specifics on how to load a constant to an arm register. This step must always be done to get the address of a global variable. The compiler will not know a prior how far away a global is. If you use gcc with -lto or use something like whole-program, then better optimizations can be performed. Basically, these will transform the global to a static.

下面一个编译器可保持的寄存器用的全球碱的地址的,然后不同的变量装载有一个偏移;如 LDR RN,[RX,#offset] 。也就是说,如果你是幸运的。

Here a compiler may keep a register with the address of a global base and then different variables are loaded with an offset; such as ldr rN, [rX, #offset]. That is, if you are lucky.

的RISC处理器,如ARM支持加载/存储单元,其处理所有的存储器访问的设计。通常情况下,加载/存储指令能够在的[注册+偏移] 的形式。此外,所有的RISC寄存器是近似对称。这意味着任何寄存器可以用于此的偏移的访问。通常情况下,如果你传递一个结构或数组的指针作为参数,那么它就变成了同样的事情。也就是说, LDR RN,[RX,#offset]

The design of RISC CPUs, like ARM support a load/store unit which handles all memory accesses. Typically, the load/store instructions are capable of the [register + offset] form. Also, all RISC registers are approximately symmetric. Meaning any register can be used for this offset access. Typically, if you pass a struct or array pointer as a parameter, then it becomes the same thing. Ie, ldr rN, [rX, #offset].

现在,该参数的优势在于,最终你的程序可以通过传递不同的指针支持的多个阵列结构的。此外,它给你的优势,共同组数据一起赋予的缓存的好处。

Now, the advantage of the parameter is that eventually, your routines can support multiple arrays or structures by passing different pointers. Also, it gives you the advantage to group common data together which gives cache benefits.

我想指出,全局都在ARM有害的。您应该只使用的全球指针的,你的code需要的的。或者你有某种同步内存。即,全局只应使用全局功能,而不是用于数据

I would argues that globals are detrimental on the ARM. You should only use global pointers, where your code needs a singleton. Or you have some sort of synchronization memory. Ie, globals should only be use for global functionality and not for data.

传递的所有值经由堆栈显然是高效和射门一个存储器引用或指针的值。

Passing all of the values via the stack is obviously in-efficient and misses the value of a memory reference or pointer.

这篇关于是否使用全局变量增加或减少的表现,编为ARM7 C code吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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