B
bjeremy
Jon said:Why are exceptions in C++ ~6x slower than exceptions in OCaml?
Not sure.. but I bet if we buy your online book you'll tell us why.
Jon said:Why are exceptions in C++ ~6x slower than exceptions in OCaml?
IR said:What in the word "exception" (like in "exceptional", ie. "occurring
rarely", "out of the ordinary") didn't you understand?
Jon said:No error has occurred. The exception is used to short-circuit a computation.
Why not just use a goto?
Kai-Uwe Bux said:Where in the standard does it say that try-throw-catch has to be used with
exceptions? Technically, try-throw-catch is flow controll construct.
Nothing implies that it has to be slow. If it weren't for a certain coding
tradition among C++ folks, this flow construct might be used in C++ much
more frequently.
kwikius said:Why not just use a goto?
Alf said:An alternative design not using exceptions as success return values:
Node* find( Node* node, int x )
{
if( node == 0 || node->value == x ) { return node; }
Node* result;
(result = find( node->left )) || (result = find( node->right ));
return result;
}
Huh, shorter code, too! <g>
Alf said:An alternative design not using exceptions as success return values:
Node* find( Node* node, int x )
{
if( node == 0 || node->value == x ) { return node; }
Node* result;
(result = find( node->left )) || (result = find( node->right ));
return result;
}
Huh, shorter code, too! <g>
kwikius said:AFAICS its a redblack tree :
(recursion sucks too for any sort of performance ;-0)
AFAICS its a redblack tree :
(recursion sucks too for any sort of performance ;-0)
namespace detail{
inline
Node * next_node(Node *node, int x)
{
return node->value > x
? node->left
: node->value < x
? node->right
: node;
}
}
Node* find(Node* node ,int x)
{
assert(node);
Node* current_node = node;
while((current_node = detail::next_node(node,x)) != node){
node = current_node;
}
return node;
}
Alf said:Well, I didn't mean to illustrate search procedure, only exception
versus not in what I presumed to be Jon Harrop's recursive case. And I
didn't mean to illustrate performance. For that, a simple while loop,
no function calls, would suffice:
Node* find( Node* node, int x )
{
while( node != 0 && node->value != x )
{
node = (x < node->value? node->left : node->right);
}
return node;
}
Huh, shorter code, too! <g>
Jon said:Correctness first, safety second. Using a goto in this case would leak stack
because the exception is used to bail from a recursive function. The C
longjmp function can be used in this case, and it works well, but that
isn't idiomatic C++.
Huh, shorter code, too! <g>
Alf said:Node* find( Node* node, int x )
{
while( node != 0 && node->value != x )
{
node = (x < node->value? node->left : node->right);
}
return node;
}
Huh, shorter code, too! <g>
kwikius said:We are a long way from idiomatic C++ AFAICS. runtime recursion isnt
much use for anything except toy code in C++
and nor are arbitrary
exceptions. Maybe you should be using another language...
Kai-Uwe Bux said:Huh? Recursion is idiomatic in C++. As usual in imperative programming
languages, one can replace recursion by iteration for performance reasons.
The OP did exactly that, which is what triggered the first post. His OCaml
version fared much better using exceptions.
kwikius said:I rest my case...
Kai-Uwe Bux said:Wow, and that before you even made one.
Maybe, it's just because I am not a native speaker, but when did "idiomatic"
become to mean "optimized for performance"?
And when did code written
trading performance for maintainability become synonymous with "toy" code?
kwikius said:You just made it for me. ;-)
FWIW the dictionary definition of idiomatic is "peculiar to or
charcteristic of a particular language". I guess you could say
recursion is idiomatic of some arbitrary FP language, but not C++
Op's whole thrust is regarding performance, so what does he do? Use
recursion and abuse exceptions.
I suspect he knew the answer to his question all along. Perhaps he'd
have more satisfaction on a functional newsgroup using his example to
demonstrate the superiority of FP language X over C++, which I am sure
he will be able to achieve in a totally unbiased way.
In C++ don't use recursion if you can help it IMO. Its far more
efficient in code space and time to use a loop. rationale: recursion
isnt idiomatic in C++.
Jon said:Only if they are slow.
In OCaml, exceptions are so fast that they are used extensively.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.