Store global variable or recreate local variable in c?

I have been programming Java for Android for some time. Since performance is very important for the things I'm working on, I end up just spamming global variables. I guess everyone will come by now and tell me that this is the worst style in history, but let's keep it simple. For Android, local variables mean garbage collection and garbage collection - this is what kills performance.

Recently, I started using NDK. Now I feel like actually taking all the local variables and changing them to global variables. I am wondering if that makes sense in c code. Obviously, this is not a good style, but if it is necessary for speed, I will readily sacrifice the style.

I looked at old threads about local and global, but I could not find anything about speed. So my question is that if I call a function very often, is it because local variables are created and die after the function is executed? Or it doesn’t matter at all, and I can happily continue to use local variables.

I would have experienced it myself, but for some reason, the performance of my application rises and falls, like on a roller coaster, and I doubt that I can really understand the meaning of the data. I hope someone can help me before I rewrote the whole code :)

+6
performance c android-ndk
source share
4 answers

In C, the performance difference is hardware dependent. Loading the global on the RISC processor is more instructions (because you have to load both half of the address in separate instructions, as opposed to adding a stack pointer), and then you need to deal with cache problems. For the most part, you can expect your local variables to be in the cache. Using globals will slightly outperform the cache, and some features can be very affected.

If you have significant performance variability when you run the application, it is likely that your statement about the effect of local variables is irrelevant.

The "cost" of creating a local variable in C is zero; it just flips case (stack pointer) to make space for local. Then you initialize this variable by any means. You need to know whether it is expensive or not, in random order. When the function completes, the stack pointer returns to its previous value, regardless of how many local variables you have.

If your definition of "local variables" is objects with allocated heaps, you will suffer from the cost of allocating memory. In my opinion, memory allocation is very slow, so whatever you choose to get away from malloc / free (and the “new” in Java) is better. (I make games, and we usually use dlmalloc, but even this is too slow for regular use, 400ns per call is added quickly.)

+8
source share

For Android, local variables mean garbage collection ...

This is the wrong operator. Local variables are allocated on the stack, and not dynamically allocated on the heap. Check this article for what gets highlighted where in Java

As a rule, the elements allocated on the stack do not require garbage collection / release and "die" immediately after completion of the current area. Stack allocation / deallocation is much faster than heap allocation and garbage collection.

Try to avoid global variables in both style and performance. Local variables allocated on the stack will execute much faster.

+11
source share

On the MIPS and ARM processors found on most Android phones, there is no reason to move local variables to the global “for performance” space. Locales are stored on the stack, and the stack distribution is one operand; in addition, the entire stack is cleared immediately when ret called. Moving them into a global space will simply turn your logic into a buried mess of insoluble state without any advantages.

The only place to worry about creating objects by creating objects is when you place them on the heap (for example, using malloc() ). This is exactly where C is “more advanced than garbage-collected languages,” because you can see and control exactly when these mallocs occur and when they are freed. Actually, C malloc() not faster than Java new ; rather, because each distribution is transparent and explicit to you, you can do the necessary work to make sure that such slow operations occur as little as possible.

+2
source share

By the way, declaring a static variable inside a C function will give you global behavior without clogging up the global namespace.

But, as already mentioned, the declaration of automatic variables on the stack takes 0 times, and access to these variables is also very fast, so there is no reason to refuse local function variables.

If you really need this extreme level of optimization, you should take a look at all your usual functions to avoid overhead.

0
source share

All Articles