Re: To go with Go or C/C++?
???? Tiib wrote:
This is easy to test.
It is.
<code>
#include <cstdio>
#include <ctime>
int ms_elapsed( clock_t start, clock_t stop )
{
return static_cast<int>( 1000.0 * ( stop - start ) / CLOCKS_PER_SEC
);
}
bool checkThingsWithIf( int param )
{
return param % 10000 != 0;
}
void checkThingsWithTry( int param )
{
if ( param % 10000 == 0 )
{
throw true;
}
}
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;
}
void benchmark( int (*foo)(int))
{
clock_t volatile start;
clock_t volatile stop;
int volatile sum;
start = clock();
sum = 0;
for ( int i = 0; i < 1000000; ++i )
{
sum += (*foo)( i );
}
stop = clock();
printf( "It took %d msec; (sum %d)\n", ms_elapsed( start,stop) , sum
);
}
void benchmark( int (*foo)(int))
{
clock_t volatile start;
clock_t volatile stop;
int volatile sum;
start = clock();
sum = 0;
for ( int i = 0; i < 1000000; ++i )
{
sum += (*foo)( i );
}
stop = clock();
printf( "It took %d msec; (sum %d)\n", ms_elapsed( start,stop) , sum
);
}
int main( int argc, char* argv[] )
{
benchmark( &testWithTry);
benchmark( &testWithIf);
}
</code>
<output>
rui@kubuntu:tmp$ g++ -O0 main.c++ && ./a.out
It took 2130 msec; (sum 197990000)
It took 2280 msec; (sum 197990000)
rui@kubuntu:tmp$ g++ -O1 main.c++ && ./a.out
It took 1150 msec; (sum 197990000)
It took 1050 msec; (sum 197990000)
rui@kubuntu:tmp$ g++ -O2 main.c++ && ./a.out
It took 1160 msec; (sum 197990000)
It took 670 msec; (sum 197990000)
rui@kubuntu:tmp$ g++ -O3 main.c++ && ./a.out
It took 700 msec; (sum 197990000)
It took 670 msec; (sum 197990000)
</output>
Rui Maciel