Let's say there is a function that I need to call millions of times. Arithmetic operations performed by this function are not so heavy, so the only thing that matters is how fast all variables are allocated. Also we assume that the variable is always allocated on stack. The simplest case example:
void doSomething(){
int aReallyLargeVariable[10000];
...performing a few light operations on that variable and returning nothing...
}
I know that when function returns all it's variables are destroyed,so
wouldn't it be better to cache this variable by making it static or global? What would be the best way to optimize it?
It is not the allocation that will cause performance problem. The problem is to initialize it so when
int aReallyLargeVariable[10000];
won't take many time the
int aReallyLargeVariable[10000] = {0};
will do. Also creating huge objects dynamically can cause a problem.
If you have a function that has not very heavy logic and is using only primitive types just define it as inline
and do not worry about performance.
If you need to define big amount of object think about another data structure like stack
or vector
that won't need to have 1000 or more elements
To optimize this function, considering it is called many times, the first step would have to be do not declare the large variable locally
. When you do that, a few things happen:
You are better off declaring this array somewhere else (declare it globally if you have to) and pass in a pointer to the array. This way you can also reuse the memory and not waste time reallocating.
I would suggest to allocate them as static so that the entire int aReallyLargeVariable[10000];
wont bother the stack by allocating and deallocating this huge amount of memory every time the function is called. However, you can also declare the int aReallyLargeVariable[10000];
as global, but many developers hate a lot of global variables in their code. If you don't like static variables in functions (due to concurrent execution or so) and don't like global variables either, then you can declare the variable as static in the global scope so that the variable name only be valid in the context of its declaration which can prevent linker errors.
Source.cpp
static int aReallyLargeVariable[10000];
void myFunction()
{
aReallyLargeVariable[1] = 10;
}
Object.cpp
int aReallyLargeVariable[10000]; /* No name collisions during linking */
Note: It only works as long as you don't decide to include the Source.cpp in Object.cpp or vice verse
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.