Thread: How slow is exit()?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    How slow is exit()?

    How slow is exit()? Is it better to use exit() or return up the stack to main()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I can't see the point of optimising something which only happens once.

    There's quite a lot of work goes on, closing files, freeing memory, removing entries from process tables, updating accounting information (say) and informing parent processes, all of which take a lot longer than how you get there in the first place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, I was just wondering if I should unravel the stack or not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well there's no absolute 'need' to, but you might consider that if you want your software to be more re-usable, having it call exit() from within it might make it a lot harder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The C standard guarantees that exit() will have the same behaviour as the final return from main, so there isn't going to be much difference.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, thanks, maybe I'll start using it, then.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Ah hem....

    The C standard requires that calling exit(some_value) has the same net effect as returning some_value from main. In other words, the environment in which your program is executed (eg the shell under unix) will receive some_value as a status value.

    That is not the same as saying that calling exit() from some function will be more (or less) efficient than returning "up the stack" to main(), which then returns the value. The relative performance will, in general, depend on your compiler and library --- although, instinctively, I'd guess that calling exit() would be faster as it is not necessary to do checks in each caller. It is also not something that I'd worry about: if your program needs to exit, then a few extra machine instructions is not significant.

    Although not relevant to C, calling exit() will cause problems for some C++ programs, as the process of stack unwinding has side effects in C++ (eg invoking destructors of objects that are local to every function in the call stack).

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or here's a nutty idea...TRY IT! Just write a little script to run your program a few dozen times (depending on the speed of your computer) and then run your script and time it. Then change your program to the other method and repeat. If you're using linux you can do 'time <your script name>'...under Windows, not sure.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I wrote my own program to time other programs, but it uses time() and isn't too accurate.

    Okay . . . about three times should be good enough for my computer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    dwks: these 'how fast is x' questions are completley radiculous. Please think about what you are asking. Why the heck does it matter hwo fast 'exit' is? It's exiting yoru dam program, who cares?

    Use a profiler to time a program, that is what they are there for. and time() A) is not precise B) does not tell you ho wmuch time was spent IN your code.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    How slow is exit()? Is it better to use exit() or return up the stack to main()?
    I agree with other posters about speed -- its irrelevent. As for which is better? Depends. if you do a lot of dynamic allocation in main() when you SHOULD deallocate them before exiting the program. Most operating systems, however, will clean up (such as wipe you nose) for you. But for those os that don't, the program will have to clean up itself. Otherwise, returning from main() is identical to calling exit() from someplace else. Myself, I never call exit() from main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat Program Help?
    By Paul22000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 02-10-2009, 12:35 AM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. exit() ?
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 04:31 PM