functions, functions, and more functions! 
Author Message
 functions, functions, and more functions!

      I`m programming a very complicated program, and have begun to notice that
I am creating and using A LOT of functions.  So I`m just wondering, what do
functions do to the program`s size, speed, etc.?  Do the more functions you use
and create make the program larger in size?  Slow the program down? Speed the
program up??
Thanks,
Brian  



Mon, 04 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!


Quote:
>      I`m programming a very complicated program, and have begun to notice
that
>I am creating and using A LOT of functions.  So I`m just wondering, what do
>functions do to the program`s size, speed, etc.?  Do the more functions you
use
>and create make the program larger in size?  Slow the program down? Speed
the
>program up??

Yes.  Except when they don't.
You seem to be concerned about speed.  Use a profiler. [FCOL].
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Mon, 04 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!


Quote:

> I`m programming a very complicated program, and have begun to notice that
> I am creating and using A LOT of functions.  So I`m just wondering, what
> do functions do to the program`s size, speed, etc.?  Do the more functions
> you use and create make the program larger in size?  Slow the program
> down? Speed the program up??

Dividing a complex task up into a lot of easy-to-understand functions is
generally seen as good coding style.  I think you're doing the right thing
if you have a lot of functions.

Regarding space: the compiler has to generate code for each function you
define, but that space would be used even if you put the text of the
function in the calling environment.  If you have a single action that is
performed in a number of places, then space can be saved by breaking that
action out into a separate function.  (But also think about the compiler's
function inlining; I'll come back to that below.)

Regarding speed: performing a function call requires some time.  Arguments
must be copied to some known location (typically the stack), the current
execution location must be saved, and the processor must jump to the right
place.  On function exit, the processor must jump to the saved execution
location, and if the arguments were placed on a stack, the stack must be
fixed up.  [This is a slight simplification of function call mechanisms on
real-world machines.]  Many people claim that function calls therefore slow
your program down.  I disagree -- for one thing, function call overhead is
usually minimal in the context of a larger program.  In addition, many
compilers can automatically inline function calls: when appropriate, they
put extra copies of the code of a function in places where that function is
called, to avoid the function call overhead.  The difficult thing is to
decide when it is appropriate, and compilers often provide switches to
change their behaviour in this regard.

So, the answer to your question is that it's difficult to decide, especially
in the abstract.  I would recommend that the clarity gained by splitting a
program into many functions outweighs the possible performance gains of
hand-inlining the code.  Ultimately, you have to decide whether space or
speed is the issue: if space, then you should prevent the compiler from
inlining function calls.  If speed, then you should tell the compiler to
inline as many function calls as possible.  But I wouldn't optimise for
either of these conditions without profiling your program to see where time
is being spent.

--



Mon, 04 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

Quote:

>       I`m programming a very complicated program, and have begun to notice that
> I am creating and using A LOT of functions.  So I`m just wondering, what do
> functions do to the program`s size, speed, etc.?  Do the more functions you use
> and create make the program larger in size?  Slow the program down? Speed the
> program up??
> Thanks,
> Brian

This all depends on the linker that you use.

Functions, and any code, add to the size of your program only if they
are used.  Good linkers will filter out the functions that are not used.

As far as execution speed goes, subroutines (functions) will slow down
the program vs. putting them all in the main() function.  However, your
main() function will become incomprehensible and difficult to maintain.
There is a point where a function call will use more execution time and
speed than "inlining" the same code.

Most people create functions based on themes.  This helps speed up the
DEVELOPMENT time.  Nowadays, execution speed is not as important as
development time.  The cost it takes for a $70k/year programmer to
optimize a program to gain 10% better execution time is not worth the
cost (of not doing it at all).  Also, program size is not as import as
it was 20 years ago; the importance of program size is diminishing with
the reduction in cost of memory and external storage (harddrives, etc.).

To peak your curiosity, have your compiler translate one of your
function modules into assembly to see how much overhead is used before
the first line of your function is executed.

If your really worried about speed & size, optimize your design; reduce
requirements if you have to.  Only optimize the code when the former
methods don't yield any improvements.

--
Thomas Matthews



Mon, 04 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

Quote:

>      I`m programming a very complicated program, and have begun to notice that
>I am creating and using A LOT of functions.  So I`m just wondering, what do
>functions do to the program`s size, speed, etc.?  Do the more functions you use
>and create make the program larger in size?  Slow the program down? Speed the
>program up??

Rather obviously, each function you write takes up space. However, if you
were to write your program as one single huge main() with no function
calls anywhere, you'd use nearly as much space. The exact amount of
overhead involved with a function call is up to the compiler and OS you
use. I suppose it could even be less than a goto on some obscure system
I've never heard of, but it's likely to be some small but measurable number.

This 'function call overhead' does add to the program's size and cost in
speed.  However, the size overhead is an illusion: if a function is
called more than once, it's usually a savings. Function calls DO
occasionally cost something in terms of execution speed, but even very
time-critical applications find their advantages to outweigh the costs in
almost all situations.

By the way, if speed *is* critical, there are ways to tell many modern
compilers to un-function-ize your code (aka inlining). So don't go about
abandoning the ease of development and maintenance in order to save a
microsecond or so in a tight loop -- unless that microsecond will make a
real difference to the application as a whole.

--
  Bob           One good thing about being wrong is the joy it brings to others



Mon, 04 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

Quote:

>      I`m programming a very complicated program, and have begun to notice that
>I am creating and using A LOT of functions.  So I`m just wondering, what do
>functions do to the program`s size, speed, etc.?  Do the more functions you use
>and create make the program larger in size?  Slow the program down? Speed the
>program up??

I wouldn't be concerned about speed or size. The question is whether
decomposing a particular problem into a group of functions makes the
complexity of the code increase of decrease.

--
Craig

Manchester, NH
If you have enough spiritual power to levitate a car, what
difference does it make what religion you call yourself?
  -- "Stella"



Tue, 05 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!


Quote:
>      I`m programming a very complicated program, and have begun to notice that
>I am creating and using A LOT of functions.  So I`m just wondering, what do
>functions do to the program`s size, speed, etc.?  Do the more functions you use
>and create make the program larger in size?  Slow the program down? Speed the
>program up??

As far as a program's *size* is concerned, the more code, the bigger
the program. It doesn't really matter much if the code grows because
of adding functions, or adding code to existing functions. Also, the
speed of a program is a function of what the program *does*, not a
function of how many functions make up the program. There are a couple
of areas, however, where the number of functions *is* related to
speed. That is in debugging and maintainance. Having many small,
well-documented and easily understood functions as opposed to one or a
very few large functions makes debugging *much* easier, and therefore
faster. (it also makes the likleyhood of a massive debugging session
diminish, since writing smallish functions greatly increases the
chances of writing the function correctly in the first place)

Also, a year or two from now, you'll wake up in the middle of the
night with a brilliant idea for an addition/modification to your
program. You'll find that not having to deal with a 5000-line main()
function will be a blessing.

I haven't mentioned how big a function should be before it's
considered "too big", because this is somewhat of a religious issue.
But, personally, I begin to get suspicious when the function grows
much beyond a screenfull. I suppose one's own definition of "too big"
has something to do with one's short-term memory capabilities. I have
no trouble remembering Arthur Godfrey's description of FDR's funeral
procession, but have a great deal of trouble remembering exactly what
I did this morning.

At some point you will probably want to consider breaking up your
source file into two or more (sometimes *many* more) seperate files
for all of the above reasons.

--

Cameron Foster

http://www2.netcom.com/~cdfoster/



Tue, 05 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

Quote:

>       I`m programming a very complicated program, and have begun to notice that
> I am creating and using A LOT of functions.  So I`m just wondering, what do
> functions do to the program`s size, speed, etc.?  Do the more functions you use
> and create make the program larger in size?  Slow the program down? Speed the
> program up??
> Thanks,
> Brian

To a reasonable first approximation:  The number of functions has
no effect on execution speed.  If a piece of code is used
a lot making it a function will decrease executable size,
otherwise there is no effect.

Do you really care about executable size?

Remember: Functions are a Good Thing.  The more the better.

Now, it is true that a function call in an inner loop can
add overhead and slow a program down.  Or perhaps
not.  It depends on so many factors there
is no use trying to guess ahead of time.
Write the program, use good algorithms, get
it working right.  Then if execution speed is
an issue:
       *************** PROFILE!!!!!!!!!!!****************
It will turn out that 90% of the code has no
practical effect on run speed.  It may turn
out that 99% of the code has no practical effect
on run speed.  It will probably turn out that
the bit of code that has the most effect on run speed
is not the bit of code you expected.
When you know where the bottlenecks are
then experiment.

                             -William Hughes

First rule of code optimization:  Don't Do It

First rule of code optimization (for experts): Don't Do It Yet



Tue, 05 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

: >       I`m programming a very complicated program, and have begun to notice that
: > I am creating and using A LOT of functions.  So I`m just wondering, what do
: > functions do to the program`s size, speed, etc.?  Do the more functions you use
: > and create make the program larger in size?  Slow the program down? Speed the
: > program up??
: > Thanks,
: > Brian

Functions are absolutely a necessity.  Also, you don't want your functions
to be to large.  If they are very large than more than likely they can be
further split into smaller functions.  This is the only, and best way to
manage a large software project.

Yes, lots of functions will slow the program down some.  Every time a
function is called, a context switch is done where the next line of code
in the sequence must be remembered, the called functions local variable
must be created, etc.

If speed is absolutely critical you can make the functions inline which
will tell the compiler to actually put the functions code exactly where
you call it, this saves the context switch but it increases overall code
size.  

Code reuse is usually the way to go and you will want to create general
functions that do things which can be used in various parts of your
program.  This way, you are free to concentrate on getting the program to
operate correctly.  Its easier to focus on the meaning of your code.



Tue, 05 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

On Fri, 19 Jun 1998 02:05:48 +0000, William Hughes

Quote:

>Do you really care about executable size?

Don't you ? If not, go work for Microsoft.

--
Sylvain Poirier

___
C is mother, C is father.



Wed, 06 Dec 2000 03:00:00 GMT  
 functions, functions, and more functions!

Quote:

> Yes, lots of functions will slow the program down some.  Every time a
> function is called, a context switch is done where the next line of code
> in the sequence must be remembered, the called functions local variable
> must be created, etc.

Not necessarily.

Quote:
> If speed is absolutely critical you can make the functions inline [snip]

A good optimising compilers can and will do this automatically. That's
why I made that comment above. And you can not make a function inline
in standard C, because this is not supported by the language.

What you say applies to a lot of compilers and contexts, But *not* to
all in the absolute sense that you say it. And certainly not in the
context of standard C.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 08 Dec 2000 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Call a function in a function with name of a function as parameter

2. How to use (pointer to function), and function and pointer to (pointer to function)

3. Call C++ member function from C function ?

4. Class function pointer does not evaluate to a function

5. declaring functions inside other functions

6. function of a function and variable declaration...

7. Question : A generic function to retrieve the call parameters of a function

8. function returning a function ptr

9. Function returning a function-pointer - how to prototype ?

10. Intialize the list of functions with runtime library common function

11. Is there a similar function in C# to the shell function in VB

12. Consequences of using an external function without an external function declaration

 

 
Powered by phpBB® Forum Software