in reply to How to select the code on basis of memory usage and execution time.

If you have the same test every time, it is more efficient memory-wise to have the test in the sub-routine, but that will carry a small loss of efficiency in running the program as far as execution time goes because you will call the subroutine and only then test if it was necessary.

In both cases however, the difference will be unnoticeable for all practical purposes unless you call the subroutine over-and-over again.

Personally I would say, go with what feels most natural for yourself. If the whole of the subroutine depends on this flag, I would leave the test outside of the subroutine as that indicates more clearly --IMHO-- that the running of the subroutine depends on the value of the flag

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

  • Comment on Re: How to select the code on basis of memory usage and execution time.