Re: To go with Go or C/C++?
On Friday, 3 May 2013 19:09:41 UTC+3, Scott Lurndal wrote:
=D6=F6 Tiib <ootiib@hot.ee> writes:
Also C is less efficient in the situation. When the case where
further processing is impossible occurs very rarely (is actually
exceptional) then all the eliminated error checking code in C++
makes it lot faster than same thing written in C.
For this, you must offer more evidence than simple assertion,
I'm afraid.
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, s=
top ), 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.