I wrote two simple programs.... one using globals and one using local variables with pointers.
The global var program ran about twice as fast as the local var program on my cpu.
Code:
//globalTime
#include <iostream>
#include <ctime>
void calcTwo( void );
//globals
long double One, Two;
using namespace std;
int main ( void )
{
long double start, end;
start = clock();
for( One = 0; One < 1000000; One++){
calcTwo();
}
end = clock();
cout << ( (long double)( end - start ) / (long double)CLOCKS_PER_SEC ) << " seconds" << endl;
return 0;
}
void calcTwo(void){
Two = (One * One * One + One) / (One * One + One);
}
FreeBSD # ./globalTime
0.03125 seconds
Code:
//localTime
#include <iostream>
#include <ctime>
void calcTwo( const long double * One_ptr, long double * Two_ptr );
using namespace std;
int main ( void )
{
long double start, end, One, Two;
start = clock();
for( One = 0; One < 1000000; One++){
calcTwo(&One, &Two);
}
end = clock();
cout << ( (long double)( end - start ) / (long double)CLOCKS_PER_SEC ) << " seconds" << endl;
return 0;
}
void calcTwo( const long double * One_ptr, long double * Two_ptr ){
*Two_ptr = (*One_ptr * *One_ptr * *One_ptr + *One_ptr) / (*One_ptr * *One_ptr + *One_ptr);
}
FreeBSD # ./localTime
0.0625 seconds
They both ran faster if I took out the function call and did the calculation in main()... but the global var program was still about twice as fast. I am not sure if this was a good representation of a global program vs. a local program or not... but it was interesting.