abusing exceptions
-
hi, i would like to use exception handling as a non local goto mechanism so i can implement tail calls, but its very slow (msvc 7.1, release build). can anybody tell me why, whether this is a blind alley, and how to speed things up if it isn't? cheers jono day-one.com code follows: ==================================================================================== #include enum{SZ=1000}; int integers[SZ]; using std::ostream; using std::cout; struct Continuation { Continuation(int cont) :cont_(cont) {} int cont_; }; ostream &tail_call_helper(ostream &os, int &i) { os << integers[i++] << ' '; if(i < SZ) { throw Continuation(i); } else { // do nothing } return os; } ostream &tail_call_(ostream &os) { int i = 0; while(true) { try { tail_call_helper(os, i); } catch(Continuation c) { i = c.cont_; continue; } break; } return os; } int main(int argc, char* argv[]) { tail_call_(cout); return 0; }
-
hi, i would like to use exception handling as a non local goto mechanism so i can implement tail calls, but its very slow (msvc 7.1, release build). can anybody tell me why, whether this is a blind alley, and how to speed things up if it isn't? cheers jono day-one.com code follows: ==================================================================================== #include enum{SZ=1000}; int integers[SZ]; using std::ostream; using std::cout; struct Continuation { Continuation(int cont) :cont_(cont) {} int cont_; }; ostream &tail_call_helper(ostream &os, int &i) { os << integers[i++] << ' '; if(i < SZ) { throw Continuation(i); } else { // do nothing } return os; } ostream &tail_call_(ostream &os) { int i = 0; while(true) { try { tail_call_helper(os, i); } catch(Continuation c) { i = c.cont_; continue; } break; } return os; } int main(int argc, char* argv[]) { tail_call_(cout); return 0; }
Hi, the first exception when running under Visual Studio is extremely slow. All others are just slow, because they need to create an exception object (which includes a stack traceback), then scan the stack for a matching catch. After all, exceptions are intended to take care of the exceptional case; for normal cases yes they are a blind alley. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Hi, the first exception when running under Visual Studio is extremely slow. All others are just slow, because they need to create an exception object (which includes a stack traceback), then scan the stack for a matching catch. After all, exceptions are intended to take care of the exceptional case; for normal cases yes they are a blind alley. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
thanks for that Luc. so, any suggestions then? i seem to recall doing something similar to this with setjmp and longjmp a few years ago, but i'd be loath to try it with my boost heavy code. i'd be interested in any good resources on tail calls in C++. i've found one or two online, but after the first paragraph the discussion degenerates into 'real' languages like lisp & scheme, lol. cheers jono