I have a program that involves a lot of calls to a handful of functions, each of which locally allocates fixed sized arrays (about a few hundred bytes total). Is it correct to assume that moving all the allocations to main and then passing pointers will get better speed? In other words, does subtracting from the stack pointer take linear or constant time, and, if it takes constant time, what's the cost compared to passing a pointer to a function?
I did a small speed test. Example #1 runs a little faster.
Example #1
using namespace std;
#include <iostream>
int f(int* a){
// do stuff
return 0;
}
int main(){
int a[1000];
int x;
for (int i = 0; i < 50000; ++i){
x=f(a);
}
return 0;
}
Example #2
using namespace std;
#include <iostream>
int f(){
int a[1000];
// do stuff...
return 0;
}
int main(){
for (int i = 0; i < 50000; ++i){
x=f();
}
return 0;
}