How slow is exit()? Is it better to use exit() or return up the stack to main()?
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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().Originally Posted by dwks