Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
A simple benchmark that tests the speed of local
versus member variables versus global variables.
The following tests are performed:
Avoid using global data [1-4].
Technical facts
Application type(s)
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
STL: GNU ISO C++ Library, version 4.7.2
TEMPLATE = app
CONFIG += console
CONFIG -= qt
QMAKE_CXXFLAGS += -Wextra -Werror
SOURCES += main.cpp
|
main.cpp
Results
On a laptop (built in around 2011) with Lubuntu:
906192
906192
906192
906192
Globals: 7.84 seconds.
Members only: 5.9 seconds.
Members with local variables: 5.78 seconds.
Locals : 5.8 seconds.
|
- Andrei Alexandrescu. Modern C++ Design. 2001. ISBN: 0201704315. Item 10: 'Minimize global and shared data'.
- Andrei Alexandrescu. Modern C++ Design. 2001. ISBN: 0201704315. Item 18: 'Declare variables as locally as possible'.
- Stephen C. Dewhurst. C++ Gotchas. 2003. ISBN: 0-321-12518-5. Gotcha #3: 'Avoid global variables'.
- C++ FAQ Lite: 'The names of global variables should start with //' and 'Instead of using a global variable, you should seriously consider if there are ways to limit the variable's visibility and/or lifetime'.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml