Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
To go with Go or C/C++?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Öö Tiib, post: 5090746"] This is easy to test. For example two functions one returns bool that is false very rarely, one throws very rarely (not meant as example of good code): bool checkThingsWithIf( int param ) { return param % 10000 != 0; } void checkThingsWithTry( int param ) { if ( param % 10000 == 0 ) { throw true; } } Too simple so compilers want to inline those, make sure they don't. Now just write two test functions. To keep it simple I won't throw far and deep like usual, just replace 200 ifs in cycle with one try/catch: int testWithIf( int param ) { int ret = 0; for ( int j = 0; j < 200; ++j ) { if ( !checkThingsWithIf( param+j ) ) { return ret; } ++ret; } return ret; } int testWithTry( int param ) { int ret = 0; try { for ( int j = 0; j < 200; ++j ) { checkThingsWithTry( param+j ); ++ret; } } catch ( bool ) { } return ret; } The stuff is fast so lets run the tests million times. I write it as C-ish as I can ... #include <cstdio> #include <ctime> int main( int argc, char* argv[] ) { clock_t volatile start; clock_t volatile stop; int volatile sum; start = clock(); sum = 0; for ( int i = 0; i < 1000000; ++i ) { sum += testWithIf( i ); } stop = clock(); printf( "With if it took %d msec; (sum %d)\n", ms_elapsed( start, stop ), sum ); start = clock(); sum = 0; for ( int i = 0; i < 1000000; ++i ) { sum += testWithTry( i ); } stop = clock(); printf( "With try it took %d msec; (sum %d)\n", ms_elapsed( start, stop ), sum ); } Output with Visual studio 2010 (run it *without* debugging otherwise it heavily hooks itself to exceptions): With if it took 921 msec; (sum 197990000) With try it took 782 msec; (sum 197990000) So 17% better performance thanks to replacing 200 ifs in cycle with one try/catch. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
To go with Go or C/C++?
Top