I'm implementing an algorithm and the computational flow is a somewhat
deep. That is, fcn A makes many calls to fcn B which makes many calls
to fcn C, and so on. The return value of the outermost fcn is a boolean
and there are certain places within the inner functions where it may
become apparent that the return value is false. In this case I'd like
to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an
exception if the return value is ever determined early like this. Then
within fcn A I'll catch the exception and return false. Is this a good
approach or is there a more C++ way to handle this? It's sort of a
glorified goto, but it does get the job done.
On a related note, if an exception is never thrown, is there any
performance penalty to enclosing a block of code within a try-catch
block. The exceptional case above should be rare and I don't want to
hurt the common case by doing this.
Thanks,
Mark 40 3142
* Mark P: I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an exception if the return value is ever determined early like this. Then within fcn A I'll catch the exception and return false. Is this a good approach or is there a more C++ way to handle this?
In some rare cases it can be a good approach.
It's sort of a glorified goto, but it does get the job done.
On a related note, if an exception is never thrown, is there any performance penalty to enclosing a block of code within a try-catch block. The exceptional case above should be rare and I don't want to hurt the common case by doing this.
Measure -- that's the /only/ reasonable advice.
However, also see "ISO/IEC TR 18015:2006 C++ Performance - draft TR" at
<url: http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf>.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mark P wrote: I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an exception if the return value is ever determined early like this. Then within fcn A I'll catch the exception and return false. Is this a good approach or is there a more C++ way to handle this? It's sort of a glorified goto, but it does get the job done.
Did you measure how fast this implementation is, compared to a version that
returns the value back up the stack? Both activities must unwind the stack,
so (for some C++ implementations ) returning the value might be faster.
You need a better reason than "I don't feel like typing lots of return
statements" to violate the guideline in /C++ Coding Standards/ by Sutter &
Alexandrescu called "Use Exceptions for Exceptional Situations" (IIRC).
On a related note, if an exception is never thrown, is there any performance penalty to enclosing a block of code within a try-catch block. The exceptional case above should be rare and I don't want to hurt the common case by doing this.
This is a topic of many discussions, including philosophical ones over
whether a language should support exceptions at all. Google will find reams.
A more important question: Will anyone ever care if this routine is fast, or
are you just indulging in Creative Coding?
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Phlip wrote: Mark P wrote:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an exception if the return value is ever determined early like this. Then within fcn A I'll catch the exception and return false. Is this a good approach or is there a more C++ way to handle this? It's sort of a glorified goto, but it does get the job done.
Did you measure how fast this implementation is, compared to a version that returns the value back up the stack? Both activities must unwind the stack, so (for some C++ implementations ) returning the value might be faster.
You need a better reason than "I don't feel like typing lots of return statements" to violate the guideline in /C++ Coding Standards/ by Sutter & Alexandrescu called "Use Exceptions for Exceptional Situations" (IIRC).
It was this very phrase that prompted my question. I'm not sure whether
or not my situation qualifies as exceptional, but I'll consider this
further. On a related note, if an exception is never thrown, is there any performance penalty to enclosing a block of code within a try-catch block. The exceptional case above should be rare and I don't want to hurt the common case by doing this.
This is a topic of many discussions, including philosophical ones over whether a language should support exceptions at all. Google will find reams. A more important question: Will anyone ever care if this routine is fast, or are you just indulging in Creative Coding?
Thanks for the advice. Yes, speed is critically important. Think
geometry processing for large scale semiconductor designs. I don't
really care how fast the exception handling is, within reason, since it
should be rare. My question was motivated by style and coding practice
concerns.
As far as actual performance issues, I only wonder whether code which
supports exception handling, though an exception is not actually thrown,
will suffer any performance degradation. My instinct is not, since lots
of things could throw runtime exceptions and I never give these a
thought as far as affecting performance, but then as you can see I don't
really know.
Alf P. Steinbach wrote: * Mark P: I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an exception if the return value is ever determined early like this. Then within fcn A I'll catch the exception and return false. Is this a good approach or is there a more C++ way to handle this?
In some rare cases it can be a good approach.
It's sort of a glorified goto, but it does get the job done.
On a related note, if an exception is never thrown, is there any performance penalty to enclosing a block of code within a try-catch block. The exceptional case above should be rare and I don't want to hurt the common case by doing this.
Measure -- that's the /only/ reasonable advice.
However, also see "ISO/IEC TR 18015:2006 C++ Performance - draft TR" at <url: http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf>.
Great link, thanks.
Mark P wrote: As far as actual performance issues, I only wonder whether code which supports exception handling, though an exception is not actually thrown, will suffer any performance degradation. My instinct is not, since lots of things could throw runtime exceptions and I never give these a thought as far as affecting performance, but then as you can see I don't really know.
As Phlip said, this has been debated at length here. In my experience,
with my compilers, you are correct.
--
Ian Collins.
Mark P wrote: As far as actual performance issues, I only wonder whether code which supports exception handling, though an exception is not actually thrown, will suffer any performance degradation.
The answer is yes. While it's theoretically possible to write exception
handling code that does not inject any additional opcodes when no
exceptions are thrown, that's not all there is to performance. Such
implementations need data tables that must be available at runtime. That
means a bigger footprint, higher disk usage, maybe slower startup. And,
of courese, if you're not using an implementation that does that, you
may well have added code even when there no exceptions are thrown. More
important, if an exception is thrown, typical execution time is several
orders of magnitude slower than an ordinary return.
--
Pete Becker
Roundhouse Consulting, Ltd.
Mark P wrote: It was this very phrase that prompted my question. I'm not sure whether or not my situation qualifies as exceptional, but I'll consider this further.
Your situation is the normal control flow, not the exceptional
(unpredictable, uncorrectable, and risky) control flow.
Thanks for the advice. Yes, speed is critically important.
Ah, then you have lots of unit tests, and you can time them. And when they
reveal bottlenecks you can tune the code and still pass all the tests.
Think geometry processing for large scale semiconductor designs. I don't really care how fast the exception handling is, within reason, since it should be rare. My question was motivated by style and coding practice concerns.
Then the most important resource to optimize is programmer time. Write the
simple code, not the clever-clever code.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mark P wrote: I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false.
My current approach is to have any of the inner functions throw an exception if the return value is ever determined early like this. Then within fcn A I'll catch the exception and return false. Is this a good approach or is there a more C++ way to handle this? It's sort of a glorified goto, but it does get the job done.
Stroustrup has a similar example in TC++PL:
void fnd(Tree* p, const string& s)
{
if (s == p->str) throw p;
if (p->left) fnd(p->left, s);
if (p->right) fnd(p->right, s);
}
Tree* find(Tree* p, const string& s)
{
try {
fnd(p, s);
}
catch (Tree* q)
{
return q;
}
return 0;
}
Pete Becker wrote: Mark P wrote:
As far as actual performance issues, I only wonder whether code which supports exception handling, though an exception is not actually thrown, will suffer any performance degradation.
The answer is yes. While it's theoretically possible to write exception handling code that does not inject any additional opcodes when no exceptions are thrown, that's not all there is to performance. Such implementations need data tables that must be available at runtime. That means a bigger footprint, higher disk usage, maybe slower startup. And, of courese, if you're not using an implementation that does that, you may well have added code even when there no exceptions are thrown. More important, if an exception is thrown, typical execution time is several orders of magnitude slower than an ordinary return.
Thanks. Your comments and others have convinced me to choose a
different route and I have changed my code to avoid this usage.
I'm curious about one point you made, which is that an implementation
may have added code even when no exceptions are thrown. Wouldn't that
same code be generated anyway since all sorts of functions not written
by me may also throw exceptions?
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jesper Denmark |
last post by:
Hi,
Is deep serialization possible using XML. Know any good
tutorials?.
By deep serialization I mean the ability that an object
being serialized automatically serialize its members. E.g
Class A
{
|
by: Matt Kruse |
last post by:
Consider the following two functions:
function func1() {
var o ;
if ( (o=document.forms)
&& (o=o)
&& (o=o.elements)
&& (o=o.sel)
&& (o=o.options)
&& (o=o)
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |