Last active
October 16, 2022 20:21
-
-
Save martinmoene/bd290bc0ff8248de31fd to your computer and use it in GitHub Desktop.
Myth 1: “To understand C++, you must first learn C” -- performance aspect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://isocpp.org/blog/2014/12/myths-1 | |
// | |
// Bjarne Stroustrup. | |
// | |
// 2. Myth 1: “To understand C++, you must first learn C” | |
// | |
// No. Learning basic programming using C++ is far easier than with C. | |
// | |
// C is almost a subset of C++, but it is not the best subset to learn first | |
// because C lacks the notational support, the type safety, and the easier-to-use | |
// standard library offered by C++ to simplify simple tasks. | |
// | |
// Which version would you rather teach? Which version is easier to use? | |
// Did I really get the C version right? Are you sure? Why? | |
// | |
// Finally, which version is likely to be the most efficient? Yes, the C++ version, | |
// because it does not have to count the argument characters and does not use the | |
// free store (dynamic memory) for short argument strings. | |
// --- | |
// Look at the results at the bottom. | |
// Scott Meyers. std::string, SSO, and Move Semantics. Tuesday, April 10, 2012. | |
// http://scottmeyers.blogspot.nl/2012/04/stdstring-sso-and-move-semantics.html | |
// John Ahlgren. Small String Optimization and Move Operations. Friday, March 30, 2012. | |
// http://john-ahlgren.blogspot.nl/2012/03/small-string-optimization-and-move.html | |
#define SMALL 0 | |
#define MEDIUM 1 | |
#define LARGE 2 | |
#ifndef SIZE | |
#define SIZE SMALL | |
#endif | |
#ifdef __GNUC__ | |
# define MESSAGE( text ) GCC message ( text ) | |
#else | |
# define MESSAGE( text ) message ( text ) | |
#endif | |
#define TEXT1 \ | |
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. " \ | |
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, " \ | |
"when an unknown printer took a galley of type and scrambled it to make a type " \ | |
"specimen book. It has survived not only five centuries, but also the leap into " \ | |
"electronic typesetting, remaining essentially unchanged. It was popularised in " \ | |
"the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, " \ | |
"and more recently with desktop publishing software like Aldus PageMaker including " \ | |
"versions of Lorem Ipsum." | |
#define TEXT2 \ | |
"It is a long established fact that a reader will be distracted by the readable " \ | |
"content of a page when looking at its layout. The point of using Lorem Ipsum is " \ | |
"that it has a more-or-less normal distribution of letters, as opposed to using " \ | |
"'Content here, content here', making it look like readable English. Many desktop " \ | |
"publishing packages and web page editors now use Lorem Ipsum as their default " \ | |
"model text, and a search for 'lorem ipsum' will uncover many web sites still " \ | |
"in their infancy. Various versions have evolved over the years, sometimes by " \ | |
"accident, sometimes on purpose (injected humour and the like)." | |
#if __cplusplus | |
#pragma MESSAGE("Compiling as C++") | |
#include <string> | |
using std::string; | |
string compose( const string& name, const string& domain ) | |
{ | |
return name + '@' + domain; | |
} | |
int main() | |
{ | |
for ( int i = 0; i < 10000; ++i ) | |
{ | |
for ( int k = 0; k < 1000; ++k ) | |
{ | |
#if SIZE == SMALL | |
#pragma MESSAGE("Small string") | |
string addr = compose( "aa", "fb.com" ); | |
#elif SIZE == MEDIUM | |
#pragma MESSAGE("Medium string") | |
string addr = compose( "gre", "research.att.com" ); | |
#elif SIZE == LARGE | |
#pragma MESSAGE("Large string") | |
string addr = compose( TEXT1, TEXT2 ); | |
#endif | |
} | |
} | |
} | |
#else // __cplusplus | |
#pragma MESSAGE("Compiling as C") | |
#include <malloc.h> | |
#include <string.h> | |
char* compose( const char* name, const char* domain ) | |
{ | |
char* res = malloc( strlen(name) + strlen(domain) + 2 ); // space for strings, '@', and 0 | |
char* p = strcpy( res, name ); | |
p += strlen( name ); | |
*p = '@'; | |
strcpy( p + 1, domain ); | |
return res; | |
} | |
int main() | |
{ | |
for ( int i = 0; i < 10000; ++i ) | |
{ | |
for ( int k = 0; k < 1000; ++k ) | |
{ | |
#if SIZE == SMALL | |
#pragma MESSAGE("Small string") | |
char* addr = compose( "aa", "fb.com" ); | |
#elif SIZE == MEDIUM | |
#pragma MESSAGE("Medium string") | |
char* addr = compose( "gre", "research.att.com" ); | |
#elif SIZE == LARGE | |
#pragma MESSAGE("Large string") | |
char* addr = compose( TEXT1, TEXT2 ); | |
#endif | |
free( addr ); // release memory when done | |
} | |
} | |
} | |
#endif // __cplusplus | |
#if 0 | |
g++ -O2 -x c++ -std=c++03 -o main-gcc-cpp03.exe main.c | |
g++ -O2 -x c++ -std=c++11 -o main-gcc-cpp11.exe main.c | |
g++ -O2 -x c -std=c99 -o main-gcc-c99.exe main.c | |
cl -EHsc -O2 -TP -Femain-vc-cpp.exe main.c | |
cl -EHsc -O2 -TC -Femain-vc-c.exe main.c | |
g++ --version | |
timemem main-gcc-cpp03 | |
timemem main-gcc-cpp11 | |
timemem main-gcc-c99 | |
cl | |
timemem main-vc-cpp | |
timemem main-vc-c | |
-------------------------------------------------------------------------- | |
Results for SIZE == SMALL: | |
g++ (GCC) 4.8.1 | |
>timemem main-gcc-cpp03 | |
Exit code : 0 | |
Elapsed time : 13.19 | |
Kernel time : 0.00 (0.0%) | |
User time : 13.17 (99.9%) | |
page fault # : 753 | |
Working set : 2748 KB | |
Paged pool : 23 KB | |
Non-paged pool : 4 KB | |
Page file size : 544 KB | |
>timemem main-gcc-cpp11 | |
Exit code : 0 | |
Elapsed time : 12.24 | |
Kernel time : 0.02 (0.1%) | |
User time : 12.23 (100.0%) | |
page fault # : 753 | |
Working set : 2744 KB | |
Paged pool : 23 KB | |
Non-paged pool : 4 KB | |
Page file size : 548 KB | |
>timemem main-gcc-c99 | |
Exit code : 0 | |
Elapsed time : 0.89 | |
Kernel time : 0.02 (1.8%) | |
User time : 0.86 (96.3%) | |
page fault # : 693 | |
Working set : 2512 KB | |
Paged pool : 21 KB | |
Non-paged pool : 4 KB | |
Page file size : 488 KB | |
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x86 | |
>timemem main-vc-cpp | |
Exit code : 0 | |
Elapsed time : 1.31 | |
Kernel time : 0.00 (0.0%) | |
User time : 1.31 (99.9%) | |
page fault # : 514 | |
Working set : 1868 KB | |
Paged pool : 19 KB | |
Non-paged pool : 4 KB | |
Page file size : 412 KB | |
>timemem main-vc-c | |
Exit code : 0 | |
Elapsed time : 0.67 | |
Kernel time : 0.02 (2.3%) | |
User time : 0.66 (98.3%) | |
page fault # : 511 | |
Working set : 1860 KB | |
Paged pool : 19 KB | |
Non-paged pool : 4 KB | |
Page file size : 416 KB | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment