Question:
Does declaring a function as static improve the overall speed in a C project ?
?
2010-10-12 03:29:41 UTC
assume the static functions are in separate files
Six answers:
Ratchetr
2010-10-12 04:53:13 UTC
Assuming standard C, not C++ here:



You should always declare functions static when you can. Only functions that need to be called from external modules should omit static.

It's good programming practice, but is it faster?

Maybe.

On a very large project, compile and link times might be somewhat faster. The few public symbols there are, the less work the linker compiler and linker have to do.

A compiler can do more optimizations on static function than it can a non-static. For instance, it might decide to pass 1 or more parameters in registers, rather than pushing them on the stack. Or it might decide to inline the function, if it's small enough, and not even generate a real function body.



It's questionable if you would notice any real difference from this, except in a very large or very CPU intensive program, but like I said, it's good programming practice anyway.
jaberwocky6669
2010-10-12 03:34:28 UTC
I'd say no. Static allows a developer to use that method even when the class hasn't been instantiated.



OK, well, you did say C. Hmm, declaring a static variable within a function will keep that variable from being declared every time the function is called. The scope of the variable persists for the lifetime of the program. The variable isn't available to any other part of your program. It can only be used when inside of the function where the Static variable was declared. So, I'd imagine that you had an intensive calculation that only needs to be made once inside of a function then yes Static might help in speed but only if really CPU intensive.



And like Sean T said Static makes the function available only to where it was declared so, I still can't see any real improvements in speed.



Sorry for my verbosity.
Pfo
2010-10-12 07:59:47 UTC
The function calls for an instance vs. static function are the same. With the instance function, their is some overhead in creating the instance. The extra time spent instantiating an object is negligible. Use static functions for global functions not associated with objects; that's the real benefit.
2010-10-12 03:38:22 UTC
In standard C defining a function static means it is only visible within the file it is defined in, you wont get any speed improvements. You could try using inline before you function to speed things up, but it's not guaranteed, it's down to the compiler.
kopp
2016-12-01 08:13:41 UTC
Ummm, what's that?! i did no longer get bar == NULL. U propose if there is not any pointer, pointing to bar, then allocate sth new?!... nicely, why do u try this?... U understand, static variables are with u to the tip of the present internet site, available interior the function they are declared in. So no longer u nor the compiler can cut back the link between the handle of the variable and the pointer i assume. clarify extra, i've got have been given some innovations yet I dunno if u precisely propose what i think of!...
?
2010-10-12 03:47:52 UTC
right becuase you dont instanciate the class yes it will, but the over all affect will be near negligable.



cheers ian


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...