[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppGlobal
Minimize global data [1-4]. Don't pollute the global namespace. An alternative is to use the Singleton Design Pattern.
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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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.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; }
- include <iostream>
- include <cassert>
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.
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(); }
- include <math>
- include <time>
- include <iostream>
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.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(); }
- include <time>
- include <iostream>
'global' links
Code links
- #include
- scope operator'::'
- assert
- cassert (header file)
- char
- for
- #include
- int
- iostream (header file)
- main
- return
- scope operator'::'
References
- 1) Herb Sutter,Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 10: 'Minimize global and shared data'.
- 2) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Chapter 1.8.2.a: 'Don't use global data (use members)'
- 3) Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Avoid using global variables'
- 4) Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 3, paragraph 'Global variables': 'In C++, global variables because they can create very confusing code that is hard to maintain.'
- 5) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
