473,391 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,391 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 4582
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Marijn | last post by:
I'd like to know how compilers usually handle static variables that are declared inside a function (as opposed to static class-members). Like in: int counter(){ static int c=0; ++c; return c;...
6
by: J Anderson | last post by:
Greetings, I was going through the quake2 source code (as you do) when I noticed arrays are statically allocated on the stack instead of being allocated dynamically. Iíve seen this before and...
1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: =?Utf-8?B?Tmls?= | last post by:
Hi, I am facing problem with the huge Viewstate size(Using .NET 2.0). to improve the application performance, I am using Static variable to store view state data. Does anyone has other option to...
1
by: =?Utf-8?B?Tmls?= | last post by:
Hi, I am facing problem with the huge Viewstate size(Using .NET 2.0). to improve the application performance, I am using Static variable to store view state data. Does anyone has other option to...
7
by: daniel | last post by:
Hello , I always had the feeling that is better to have char arrays with the size equal to a power of two. For example: char a_str; // feels ok char b_str; //feels not ok.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.