Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Functioning In C

Reply
Thread Tools

Functioning In C

 
 
Abhishek
Guest
Posts: n/a
 
      10-06-2003
How Does Functioning(Breaking in seperate block according to the logic
using functions) a Programme effect the performance of the programme
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-06-2003
"Abhishek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> How Does Functioning(Breaking in seperate block according to the logic
> using functions) a Programme effect the performance of the programme


Negligibly. Drastically. Not at all. Context, context, context.

The organization of program logic into functions will
indeed greatly affect programmer productivity.


-Mike


 
Reply With Quote
 
 
 
 
Darrell Grainger
Guest
Posts: n/a
 
      10-06-2003
On Mon, 6 Oct 2003, Abhishek wrote:

> How Does Functioning(Breaking in seperate block according to the logic
> using functions) a Programme effect the performance of the programme


The generic answer is... it depends. It is almost guaranteed that there
will be some overhead involved with functions. This means there will be
some deduction of performance. I say ALMOST guaranteed because it is never
100% guaranteed.

On some compilers everything might become linear assembly. From a source
code view there are a lot of functions and branching. From the machine
language view everything is linear as if the program was written as one
big function called main().

In other circumstances, a function call could be translated to a branch
instruction. On processors with caching and pipelines, a branch is very
costly. Breaking a program into many small functions could double or
triple the running time of the program.

Bottom line, it depends on many factors. All of these factors are outside
the domain of comp.lang.c. What you are looking for is more in line with
specific implementations of C language. Tere should be newsgroups to help
you out if you focus on your operating system and compiler.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-06-2003
Abhishek writes:

> How Does Functioning(Breaking in seperate block according to the logic
> using functions) a Programme effect the performance of the programme


A copy of each argument must be moved to the "stack". The processor
probably has something like 'call' and 'return' as instructions designed
especially with the intent of there *being* a stack. And one of each of
those must be executed. The space for the stack must be managed which might
possibly be a single add or subtract instruction. The result, if any,
probably has to be moved someplace special. I'm thinking something of the
order of a half-dozen instructions plus one instruction per parameter.

Naturally, all of this depends on the microprocessor used and the compiler
design. The short answer is that I would not avoid using functions because
of the overhead. If you are at the point where this is a potential problem
you should probably consider switching to assembly language. The compilers
I am familiar with are not really as good at "doing" registers and getting
all the good out of them as the human mind is.


 
Reply With Quote
 
August Derleth
Guest
Posts: n/a
 
      10-06-2003
(E-Mail Removed) (Abhishek) wrote in message news:<(E-Mail Removed). com>...
> How Does Functioning(Breaking in seperate block according to the logic
> using functions) a Programme effect the performance of the programme


You really shouldn't worry about this too much unless you have to.

And you probably don't have to.

What you should worry about is how easy the program is to read,
understand, maintain, and change. Seperating it into functions greatly
helps that process. Seperating it into functions helps you break the
problem down into managable chunks, which can be easily solved one at
a time. Once all of the small problems have been solved, your big
problem has also been solved.

Finally, breaking down the problem also breaks down the solution. All
of those small solutions can be used again in a different program.
Code reuse is a great thing, and generating functions instead of just
writing one big main() is a simple, obvious means to that end.
 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      10-09-2003
> How Does Functioning(Breaking in seperate block according to the logic
> using functions) a Programme effect the performance of the programme


if the function is rather big there is no real performance loss. if you
really care about speed use a macro.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-10-2003
"cody" <(E-Mail Removed)> wrote in message
news:bm4sdc$imosl$(E-Mail Removed)-berlin.de...
> > How Does Functioning(Breaking in seperate block according to the logic
> > using functions) a Programme effect the performance of the programme

>
> if the function is rather big there is no real performance loss.


What are your premises for this statement?

> if you
> really care about speed use a macro.


What are your premises for this statement?

-Mike


 
Reply With Quote
 
NumLockOff
Guest
Posts: n/a
 
      10-10-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:a0phb.3948$(E-Mail Removed) nk.net...
> "cody" <(E-Mail Removed)> wrote in message
> news:bm4sdc$imosl$(E-Mail Removed)-berlin.de...
> > > How Does Functioning(Breaking in seperate block according to the logic
> > > using functions) a Programme effect the performance of the programme

> >
> > if the function is rather big there is no real performance loss.

>
> What are your premises for this statement?
>
> > if you
> > really care about speed use a macro.

>
> What are your premises for this statement?
>
> -Mike
>
>


What I gather from Cody's response is that
if the function is really small make a macro out of it to save the jumps and
push/pop registers.

If you have a silly func

SetTo12(int *px)
{
*px =12;
}


it is better to have the macro
#define SET_TO_12 (iii) iii=12

....

Well this example is too silly, but i hope you get the point..


 
Reply With Quote
 
Jarno A Wuolijoki
Guest
Posts: n/a
 
      10-10-2003
On Thu, 9 Oct 2003, NumLockOff wrote:

> What I gather from Cody's response is that
> if the function is really small make a macro out of it to save the jumps and
> push/pop registers.


If that were the only advantage there would be little point in inlining.
The real speedup comes from that the compiler can perform a whole flock
of other optimizations when it integrates the piece of code in an
expression.

Consider e.g.

#define cat1(x) ((x)*2+1)
#define cat0(x) ((x)*2)
unsigned foo(unsigned x) {
return cat0(cat1(cat0(cat1(cat0(cat1(x))))));
}

This is optimizable to x*64+42. (Though whether it actually happens
is a different matter..)

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-10-2003
"NumLockOff" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>
> "Mike Wahler" <(E-Mail Removed)> wrote in message
> news:a0phb.3948$(E-Mail Removed) nk.net...
> > "cody" <(E-Mail Removed)> wrote in message
> > news:bm4sdc$imosl$(E-Mail Removed)-berlin.de...
> > > > How Does Functioning(Breaking in seperate block according to the

logic
> > > > using functions) a Programme effect the performance of the

programme
> > >
> > > if the function is rather big there is no real performance loss.

> >
> > What are your premises for this statement?
> >
> > > if you
> > > really care about speed use a macro.

> >
> > What are your premises for this statement?
> >
> > -Mike
> >
> >

>
> What I gather from Cody's response is that
> if the function is really small make a macro out of it to save the jumps

and
> push/pop registers.


What "push/pop registers?" This is ISO C, remember.

> If you have a silly func
>
> SetTo12(int *px)
> {
> *px =12;
> }
>
>
> it is better to have the macro
> #define SET_TO_12 (iii) iii=12


I almost always prefer a function over a macro,
especially when side effects are involved. I try
to always only use macros to express "magic numbers."

> Well this example is too silly, but i hope you get the point..


I understand your point, but disagree with it, as
well as Cody's assertion that a macro will necessarily
give better performance than a macro.

The *only* way to accurately determine if one method
performs faster than another is to measure.

-Mike


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco 7200 Console port not functioning mdk@iping.org Cisco 5 05-26-2005 03:04 AM
Active FTP on PIX not functioning valicon Cisco 1 04-08-2005 03:41 AM
Unable to set up functioning workgroup =?Utf-8?B?RG9u?= Wireless Networking 8 02-19-2005 06:39 AM
Wireless network is functioning but icon indicates it is disconnected and status unavailable Peter Welk Wireless Networking 5 01-02-2005 02:57 PM
Aironet 1200 - Is my Radio functioning? Forrest Cisco 8 07-13-2004 12:20 AM



Advertisments