Re: To go with Go or C/C++?
In article <8cd19dc1-0e4d-4b54-871f-1f20c3e5f8c2@googlegroups.com>,
ootiib@hot.ee says...
On Friday, 3 May 2013 19:09:41 UTC+3, Scott Lurndal wrote:
???? 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):
Was that a scapegoat? I understand. Are you afraid to post code here that you
think is "good code" for you know that there are a hundred programmers just
waiting for the opportunity to pounce on it and tell you, not only why it is
bad, but why you should be a manure slinger and they should be telling you where
to sling it?
bool checkThingsWithIf( int param )
"checkThingsWithIf"
I call that "stupidcase": not CamelCase, not lowercase with underscores between
words, just stupidCase.
(Get my drift about my point above now?)
Enough digression though (I have a tendancy!), the topic is dealing with errors,
in particular, dealing with errors A idiomatic C way, compared with THE
idiomatic C++ way (note that "dealing with errors in C idiomatically" is an
oxymoron). The function signature notes that a bool is returned. So, no
indication what that return value actually is though. It could be akin to "I'll
call you tomorrow, or maybe not", or "You offended me with your suggestion, so
I'm going to punch you in the nose". No way of telling.
{
return param % 10000 != 0;
}
So what if the remainder is not zero? (I'm not going to even try to consider
what you where getting at with your example, but I think you could have chosen a
clearer one: one that isolates the error stuff from the code. As it is, you have
one of those C-like statements as the body of the function which doesn't help at
all. It should be clear even to the novice, but isn't. I mean, you wouldn't use
that as an example in a teaching setting.)
That said, fine, we have your C-way example established. (Note, again, though,
that there is no idiomatic C-way of dealing with errors and your's is
particularly weak).
void checkThingsWithTry( int param )
{
if ( param % 10000 == 0 )
{
throw true;
}
}
Throw 'true'? Surely you jest! Your example(s) now are going to the suck side of
the suckiness meter.
Oh man! I glanced at the rest of your post but was completely turned-off, but
surely you had something relevant to say that I missed IN THE FIRST FEW
PARAGRAPHS of your post, so I began responding from the beginning of it (I don't
"pre-read, then post" (else I'd be James Kanze? ;) ), but I had something of
value (IMO) to say too, so it's all good.
Too simple so compilers want to inline those, make sure they don't.
I will make sure my compiler doesn't! ;)
Now just write two test functions. To keep it simple
You wouldn't know "simple" if <I'm not good at quips, so add one here that I
would have liked to>.
I won't throw far
and deep like usual,
Football metaphor. Doesn't get points with me. I think there is an over-focus on
sports and those who like sports, make others pay for them, and that's not nice.
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;
}
Too long. You post as if you're talking to a bunch of hard-core, C-evolved, C++
programmers.
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 );
}
Were you drinking when you wrote that? Wow. It's OK, but shouldn't there
be/isn't there a separate NG for that kind of thing?
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.
Take another swig kid.