Can static variable improve efficiency?

P

Peng Jian

I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?
 
A

Allin Cottrell

Peng said:
I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?

This issue is not addressed by the C standard, so nothing is guaranteed.

Your best best, perhaps, is to write variant versions of your function,
embed them in a loop which calls them many times, and then time the
running of the program with and without the static variables (on
*nix you'd use the "time" program).

Allin Cottrell
 
J

Jack Klein

I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?

Whether using a static variable would improve efficiency depends on
many things, including the usage of the variable and the hardware
architecture of your processor. The C standard does not specify this.

If you need the variable to retain its value between one call of the
function and the next, it must have static storage duration.
Otherwise the only way to find out for sure is to code it both ways
and do timing tests.

In general, the difference should be extremely small if it exists at
all.

In practice, you probably should not worry about this until after:

1. Your program meets all of its requirements.

2. The performance of the program needs improvement.

3. A good profiling tool points to access to these particular
variables is taking a significant amount of the program's time.
 
E

Erik de Castro Lopo

Peng said:
I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables
to be static?

No, you are more likely to suffer a performance hit than an
improvement. There are a couple of reasons for this:

1) Automatic variables (ie not static) are stored on the
stack which is far more likey to be in the CPU cache
than data in static storage.

2) The value of any variable in most reasonably sized
functions is likely to be in a CPU register during the
running of a function. If that variable is not static
its value can just be discarded when the function returns
However, if it is static, it must be stored back in
static storage.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
Reporter: "What do you think of Western Civilisation?"
M.K. Gandhi: "I think it would be a good idea."
 
T

Thomas Matthews

E. Robert Tisdale said:

Yes, there may be an insignificant amount of savings.
On some platforms, local variables are allocated on a stack.
This requires additional processor instructions rather than
using a static variable whos address is fixed and already
known to the compiler. On many platforms, this requires
a couple of instructions to allocate the space and others
to restore the space. So this _could_ be an execution
speed gain. Its significance depends on the frequency
of executing the function.

I would vote that a more significant amount of execution
time could be gained by making modifications in other
areas.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
X

xarax

Thomas Matthews said:
Yes, there may be an insignificant amount of savings.
On some platforms, local variables are allocated on a stack.
This requires additional processor instructions rather than
using a static variable whos address is fixed and already
known to the compiler. On many platforms, this requires
a couple of instructions to allocate the space and others
to restore the space. So this _could_ be an execution
speed gain. Its significance depends on the frequency
of executing the function.

I would vote that a more significant amount of execution
time could be gained by making modifications in other
areas.
/snip/

It's entirely implementation dependent. Therefore,
the answer is no.
 
M

Mark McIntyre

I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?

There are three answers to this
1) yes
2) no
3) elephant

The point is., the ISO C Standard doesn't say. And in fact it will vary
from system to system, possibly even build-mode to build-mode.
 
K

Keith Thompson

xarax said:
[snip]
It's entirely implementation dependent. Therefore,
the answer is no.

It's entirely implementation dependent. Therefore, the answer is that
it's entirely implementation dependent.

If the OP had asked whether he can *portably* improve efficiency by
using static variables, the answer would be no.

It's within the realm of possibility that making a function's local
variables static could improve performance on a particular system in a
particular set of circumstances. (It's also entirely possible that it
could degrade performance.)

But to answer another question that the OP didn't ask but probably
should have:

Is it a good idea?

Probably not, unless you've already tried everything else, and you're
desperate to shave a few more cycles off your run time, and you've
actually measured the performance increase, and you're going to modify
the source in a way that makes it easy to go back to local variables
when your optimization becomes a pessimization on the next system you
port the code to, and you actually have the patience to read this
absurdly run-on sentence (which I suppose is really a run-on sentence
fragment, but I digress).

(Shorter answer: No.)
 
K

kal

I have a function that is called very very often.
Can I improve its efficiency by declaring its
local variables to be static?

Yes you can, under certain circumstances.

But the price of this improved speed can be rather steep.

The function then becomes non re-entrant(?). That is,
neither this nor other functions called by this can
call this function. This is against our intuitive
understanding of what a 'function' is, though I suspect
it does not violate any of the C specifications.

OTOH, if you have a function that spends a lot of time
allocating block(s) of memory and setting them up for
further operations then you can realize significant
performance gain by carefully declaring those blocks
as static and initializing them once.

But you may be better off using "file scope" variables,
especially if they can be declared "const".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top