472,110 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Can static variable improve efficiency?

I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?
Nov 14 '05 #1
9 4434
Peng Jian wrote:
I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be static?


No.
Nov 14 '05 #2
Peng Jian wrote:
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
Nov 14 '05 #3
On 11 Jul 2004 18:55:09 -0700, pe*********@hotmail.com (Peng Jian)
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
Peng Jian wrote:

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 no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Reporter: "What do you think of Western Civilisation?"
M.K. Gandhi: "I think it would be a good idea."
Nov 14 '05 #5
E. Robert Tisdale wrote:
Peng Jian wrote:
I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be
static?

No.


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

Nov 14 '05 #6
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Hz***************@newssvr31.news.prodigy.com. ..
E. Robert Tisdale wrote:
Peng Jian wrote:
I have a function that is called very very often.
Can I improve its efficiency by declaring its local variables to be
static?

No.


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.
Nov 14 '05 #7
On 11 Jul 2004 18:55:09 -0700, in comp.lang.c , pe*********@hotmail.com
(Peng Jian) wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #8
"xarax" <xa***@email.com> writes:
[snip]
Peng Jian wrote:

> I have a function that is called very very often.
> Can I improve its efficiency by declaring its local variables to be
> static?
[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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
kal
pe*********@hotmail.com (Peng Jian) wrote in message news:<4c**************************@posting.google. com>...
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".
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Marijn | last post: by
6 posts views Thread by J Anderson | last post: by
115 posts views Thread by Mark Shelor | last post: by
15 posts views Thread by rwf_20 | last post: by
1 post views Thread by =?Utf-8?B?Tmls?= | last post: by
1 post views Thread by =?Utf-8?B?Tmls?= | last post: by
7 posts views Thread by daniel | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.