0

Is dividing the work into 5 functions as opposed to one big function more memory efficient in C since at a given time there are fewer variables in memory, as the stack-frame gets deallocated more often?

Answer given there are a lot of local variables and the stack frames comes from a centralized main and not created on the top of each other.

I know other advantages of breaking out the function into smaller functions. Please answer this question, only in respect to memory usage.

theRealWorld
  • 1,188
  • 2
  • 8
  • 23
  • 6
    Java, Python, or C? Pick one... unless you want an exposition on the memory characteristics of each of them? – cdhowie Aug 08 '11 at 23:47
  • It's definitely more efficient developer-productivity-wise and bug-wise. So go ahead and break it up. – Keith Aug 08 '11 at 23:48
  • 3
    @Mo Zo: No, an answer to one would certainly not apply to the others accurately. The syntax of a language does not necessarily have anything to do with the runtime behaviors it shares with other languages with similar syntax. – cdhowie Aug 08 '11 at 23:50
  • You should ask yourself if your function performs more than one tasks. If so, break it up. If not, you should usually keep it together. – Tyler Crompton Aug 08 '11 at 23:51
  • @Mo Zo, Java has C syntax but is not C. – Thorbjørn Ravn Andersen Aug 08 '11 at 23:52
  • 3
    No, they're pretty different. Change the question or it may be closed. – JBernardo Aug 08 '11 at 23:53
  • 2
    C doesn't mandate how memory is implemented, so you're not free to presume that there *is* in fact a stack. In any event, this sort of worry is almost guaranteed to be misplaced; concentrate on structuring your program logically and maintainably and designing your algorithms wisely. I'm sure that the thing you're asking about is *not* going to be your bottleneck. – Kerrek SB Aug 08 '11 at 23:55
  • 2
    I've further edited your question to make it more clear and voted to re-open. Please add more specific information and the reason you havent just _tested_ this if you have it. – agf Aug 09 '11 at 00:21

2 Answers2

2

It's very language and compiler dependent.
For languages like Python where variables can be accessed dynamically (eg globals()['x']), you're effectively reducing the time for which each variable is in an accessible scope, allowing the memory to be reclaimed more often. Depending on how you do it, you may be forcing the interpreter to hold on to more scopes, and the scopes themselves can potentially use extra memory. Eg, if you're taking a chunk out of f1 and making it f2 which gets called from f1, you're increasing the stack depth & the number of scopes that have to be held onto.

For compiled languages with mature compilers like C, the memory for variables is often only used for as long as they are actually needed. So you won't be able to do much that the compiler doesn't already do.

Keep in mind that when you call a function, local variables need to be moved from registers onto the stack, as well as a return address. So in some cases, you'll end up with a bigger memory footprint if you break it apart into multiple functions called from within the original.
But also realize that if a function is only called from one place, there's a good chance that your compiler will inline it.

So in summary, it's hard to predict, and will make very little difference. Just do what is most readable.

Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62
0

So here's my philosophy. I think that, unless you're doing some crazy scientific computation that will call several different functions several millions of times (and thus overburden the system stack and lead to stackoverflow), you're good to go to break up your code (obviously don't go too crazy). But breaking up code, especially in OO code, is a godsend (especially for debugging and readability). In the end, it's all about trade offs.

Actually, now that I think of it, if you have a considerable amount of variables and the stack comes from a centralized main, most people will thread their application/program accordingly.

Since I'm not sure where you're coming from, if you're very conscious about efficiency, take a look at this question and its answers (pertaining to low level code):

How to write fast (low level) code?

If you're actually/literally talking about this enveloping all the languages you tagged, despite them all being C-based languages, they actually have very different memory/memarch models (especially since Java runs on the VM).

Community
  • 1
  • 1
Vinay
  • 6,204
  • 6
  • 38
  • 55