[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppGlobal

(C++) Global

A variable that has the largest scope possible, i.e. it can changed by all functions in the entire program. The opposite of a local variable.

  1. include <iostream>
  2. include <cassert>
int a = 10; //Global 'a' int main() { int a = 0; //Local 'a' assert(a==0); assert(::a==10); //Use scope operator to get global 'a' return 0; }


Minimize global data [1-4]. Don't pollute the global namespace. An alternative is to use the Singleton Design Pattern.

Comparing speed of globals with speed of member variables

The program below compares the speed of doing the same calculations on globals and on class members. You can observe there is significant speed difference. This is (probably) due to that the member variable addresses are not known at compile-time, whereas the global variable addresses are.

The advantage of using classes anyhow, is because of the const-correctness it can give you, e.g. the parameters in a class calles Simulation should be made const, to ensure (and show your clients) that they cannot be changed after construction.

  1. include <math>
  2. include <time>
  3. include <iostream>
class NonGlobal { public: NonGlobal(const double& max, const double& step) : mMax(max), mStep(step) {} void doStuff() { const time_t timeBegin = clock(); for(double x = 0.0; x<mMax; x+=mStep) { mA = x; mB = x * x; mC = x * x * x; mD = x * x * x; mE = x * x * x * x; mF = -x; mG = -x * x; mH = -x * x * x; mI = -x * x * x; mJ = -x * x * x * x; mK = std::sqrt(x); mL = x + x; mM = x + x + x; mN = x + x + x + x; mO = x - x; mP = x - x - x; mQ = x - x - x - x; mR = std::exp(x + 1.0); mS = 1.0 / (x+1.0); mT = 1.0 / (x+2.0); mU = x / (1.0 + x); mV = (x + x) / (x + 1.0); mW = (x + x + x) / (x + 1.0); mX = (x + x + x + x) / (x + 1.0); mY = (x * x) / (x + 1.0); mZ = (x * x * x) / (x + 1.0); } const time_t timeEnd = clock(); std::cout << "Operation took " << std::difftime(timeEnd,timeBegin) << " msecs" << std::endl; } private: double mA; double mB; double mC; double mD; double mE; double mF; double mG; double mH; double mI; double mJ; double mK; double mL; double mM; double mN; double mO; double mP; double mQ; double mR; double mS; double mT; double mU; double mV; double mW; double mX; double mY; double mZ; const double mMax; const double mStep; }; const double gMax = 100.0; const double gStep = 0.000001; double gA; double gB; double gC; double gD; double gE; double gF; double gG; double gH; double gI; double gJ; double gK; double gL; double gM; double gN; double gO; double gP; double gQ; double gR; double gS; double gT; double gU; double gV; double gW; double gX; double gY; double gZ; void doStuff() { const time_t timeBegin = clock(); for(double x = 0.0; x<gMax; x+=gStep) { gA = x; gB = x * x; gC = x * x * x; gD = x * x * x; gE = x * x * x * x; gF = -x; gG = -x * x; gH = -x * x * x; gI = -x * x * x; gJ = -x * x * x * x; gK = std::sqrt(x); gL = x + x; gM = x + x + x; gN = x + x + x + x; gO = x - x; gP = x - x - x; gQ = x - x - x - x; gR = std::exp(x + 1.0); gS = 1.0 / (x+1.0); gT = 1.0 / (x+2.0); gU = x / (1.0 + x); gV = (x + x) / (x + 1.0); gW = (x + x + x) / (x + 1.0); gX = (x + x + x + x) / (x + 1.0); gY = (x * x) / (x + 1.0); gZ = (x * x * x) / (x + 1.0); } const time_t timeEnd = clock(); std::cout << "Operation took " << std::difftime(timeEnd,timeBegin) << " msecs" << std::endl; } int main() { NonGlobal n(gMax,gStep); n.doStuff(); doStuff(); std::cin.get(); }


Comparing speed of global counters with speed of local counters

It might be, that when you declare a local variable for every for-loop, that this speeds down the program. This might be due to the creation and recreation of these local variables. To gain speed, you then might be better off using global counters instead. Here is a program that compares the speed between the two options. No significant difference can be detected. This is (probably) due to that all variable addresses are known at compile-time.

  1. include <time>
  2. include <iostream>
int gI, gJ, gX, gY, gZ; const int max = 100; int loopOneGlobals() { int sum = 0; for (gZ=0; gZ < max; ++gZ) { sum+=gZ; for (gY=0; gY<max; ++gY) { sum-=gY; for (gX=0; gX<max; ++gX) { sum+=gX; } } } return sum; } int loopTwoGlobals() { int sum = 0; for (gI = 0; gI < max; ++gI) { for (gJ = 0; gJ < max; ++gJ) { sum+=loopOneGlobals(); } sum %= (gI + 1); } return sum; } int loopOneNoGlobals() { int sum = 0; for (int gZ=0; gZ < max; ++gZ) { sum+=gZ; for (int gY=0; gY<max; ++gY) { sum-=gY; for (int gX=0; gX<max; ++gX) { sum+=gX; } } } return sum; } int loopTwoNoGlobals() { int sum = 0; for (int gI = 0; gI < max; ++gI) { for (int gJ = 0; gJ < max; ++gJ) { sum+=loopOneNoGlobals(); } sum %= (gI + 1); } return sum; } int main() { { const time_t timeBefore = std::clock(); const int x = loopTwoGlobals(); const time_t timeAfter = std::clock(); std::cout << "Time globals: " << std::difftime(timeAfter,timeBefore) << " " << x << std::endl; } { const time_t timeBefore = std::clock(); const int x = loopTwoNoGlobals(); const time_t timeAfter = std::clock(); std::cout << "Time no globals: " << std::difftime(timeAfter,timeBefore) << " " << x << std::endl; } std::cin.get(); }


'global' links

Code links

References



last edited (December 4, 2006) by bilderbikkel, Number of views: 5715, Current Rev: 18 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.