Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. abusing exceptions

abusing exceptions

Scheduled Pinned Locked Moved C / C++ / MFC
comperformancetutorialquestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quailsafe
    wrote on last edited by
    #1

    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; }

    L 1 Reply Last reply
    0
    • Q quailsafe

      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; }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      Q 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        Q Offline
        Q Offline
        quailsafe
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups