473,406 Members | 2,769 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,406 software developers and data experts.

static variables question

Hi

I have a function called from another one, which runs in loop, so that
is it called very often
something like this:

void my_function(int x) {
int temp;
temp = x * some_const;

do_something(temp);
}

suppose that x is changed really rarely ( lets say a hundreds of
calls)
would that be more efficient ?
void my_function(int x) {
static int temp;
if(temp != x * some_const)
temp = x * some_const;

do_something(temp);
}

why I'm asking is that I'm not sure, if creating temp variable in
every function call takes more time, then if statment. Anyone can
explain this to me a bit ?

thank's, have a nice day !

J.
Jul 22 '05 #1
7 1409

"Jan Bernatik" <be******@kn.vutbr.cz> wrote in message
news:24**************************@posting.google.c om...
Hi

I have a function called from another one, which runs in loop, so that
is it called very often
something like this:

void my_function(int x) {
int temp;
temp = x * some_const;

do_something(temp);
}

suppose that x is changed really rarely ( lets say a hundreds of
calls)
would that be more efficient ?
void my_function(int x) {
static int temp;
if(temp != x * some_const)
temp = x * some_const;

do_something(temp);
}

why I'm asking is that I'm not sure, if creating temp variable in
every function call takes more time, then if statment. Anyone can
explain this to me a bit ?

thank's, have a nice day !

J.


The second look less efficient, but I don't think it has anything to do with
if statements or variables. In the first example you do a multiplication
each time. In the second example you do one or two multiplications and an
equality test. So the second must be slower than the first because it always
does more work.

john
Jul 22 '05 #2
Jan Bernatik wrote:

Hi

I have a function called from another one, which runs in loop, so that
is it called very often
something like this:

void my_function(int x) {
int temp;
temp = x * some_const;

do_something(temp);
}

suppose that x is changed really rarely ( lets say a hundreds of
calls)
would that be more efficient ?

void my_function(int x) {
static int temp;
if(temp != x * some_const)
temp = x * some_const;

do_something(temp);
}

why I'm asking is that I'm not sure, if creating temp variable in
every function call takes more time, then if statment. Anyone can
explain this to me a bit ?


Count the number of multiplications you have to do in your
first version (answer: 1)
Count the number of multiplications you have to do in
your second version (answer: at least 1)

So what have you gained? Nothing at all, since in addition
to the one multipication you also have a comparison.

If you want to do some caching, do it correctly. This
means you need to store the output *and* the input to the
fomula. If the input hasn't changed then the previous
formula output can be used:

void my_function( int x ) {
static int LastX = 0;
static int LastResult = 0;

if( x != LastX ) {
LastX = x;
LastResult = x * some_const;
}

do_something( LastResult );
}

Now you have only a comparison that has to be executed in any case.
If that is faster then a single multiplication: you have to try it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
> void my_function(int x) {
static int temp;
if(temp != x * some_const)
temp = x * some_const;

do_something(temp);
}

why I'm asking is that I'm not sure, if creating temp variable in
every function call takes more time, then if statment. Anyone can
explain this to me a bit ?


For that thing to work you must initialize the static and
write the code as suggested by Karl Heinz Buchegger:

void my_function( int x ) {
static int LastX = 0;
static int LastResult = 0;

if( x != LastX ) {
LastX = x;
LastResult = x * some_const;
}

do_something( LastResult );
}

On modern processors, this is probably much slower than
computing the multiplication every time. For each local
static variable declaration, the compiler usually puts a
test to see if it's the first time that the function is
executed (in that particular case, maybe not because the
initialization can be done at compile time). Moreover, if
statements are usually very costly because of code
alignment, pipeline issues and branch prediction in the
processor. So even with three multiplications, I guess that
using static variables is slower. Do some tests and give us
your results (might be very platform and optimization
dependent though, and depends also very much on the
frequency of changes of x).

Benoit
Jul 22 '05 #4
"Jan Bernatik" <be******@kn.vutbr.cz> wrote in message
void my_function(int x) {
int temp;
temp = x * some_const;
do_something(temp);
}


The principle of caching means it may be more efficient in the following
context. You need 2 static variables -- one holding the raw data like 'x',
and the other the computation of 'x'.

void my_function(int x) {
static int prevx;
static int temp;
if (x != prevx) {
temp = x*some_const; // if this throws we don't set prevx
prevx = x;
}
do_something(temp);
}

Anyway, this is a good idiom to keep in mind, though it's usually used in
conjunction with mutable class member variables.

But in your case simply multiplying a x with a constant will surely be
faster than my version above. Multiplication is a very fast operation. My
version above has an implicit if statement to see if the variable is
constructed (though by changing the function static variables prevx and temp
to global variables you'll avoid this problem), and another if to see x !=
prevx, and probably most importantly the limitation that temp and prev won't
be stored in registers.

If the computation is anything more complex than consider the caching idiom.

Also, if the value of 'x' doesn't change much, it might be a good idea to
computer x*constant in the calling functions.
Jul 22 '05 #5
It would be more appropriate to call the function as follows,
that saves a comparison statement. Compiler is smart enough
to keep (x*some_const) in the register, if there is a loop.
void my_function(int x) {
/* int temp; */
/* temp = x * some_const; */

//loop!
do_something(x * some_const);
}

be******@kn.vutbr.cz (Jan Bernatik) wrote in message news:<24**************************@posting.google. com>...
Hi

I have a function called from another one, which runs in loop, so that
is it called very often
something like this:

void my_function(int x) {
int temp;
temp = x * some_const;

do_something(temp);
}

suppose that x is changed really rarely ( lets say a hundreds of
calls)
would that be more efficient ?
void my_function(int x) {
static int temp;
if(temp != x * some_const)
temp = x * some_const;

do_something(temp);
}

why I'm asking is that I'm not sure, if creating temp variable in
every function call takes more time, then if statment. Anyone can
explain this to me a bit ?

thank's, have a nice day !

J.

Jul 22 '05 #6
thank you guys for response.

Originally, I thought (wasn't sure) if creating temp variable every
time the function is called will take more cpu time, then declare the
variable static. And other thing is, that in theory the computation
can be a lot more complicated, so then it will be useful to use your
methods (caching both raw and computed data).

Sorry I couldn't respond earlier, I have to use google groups.

J.
Jul 22 '05 #7
Karl Heinz Buchegger wrote:
If you want to do some caching, do it correctly. This
means you need to store the output *and* the input to the
fomula. If the input hasn't changed then the previous
formula output can be used:

void my_function( int x ) {
static int LastX = 0;
static int LastResult = 0;

if( x != LastX ) {
LastX = x;
LastResult = x * some_const;
}

do_something( LastResult );
}

Now you have only a comparison that has to be executed in any case.
If that is faster then a single multiplication: you have to try it.


in a project i worked on recently, i had to do a large set of complex
calculations. a very large set. on an even larger set of input data.

one notable thing, though, is that the input data had a lot of duplicate
data points (geographically). it was pre-sorted, and these duplicate
points often only differed in two values.

i took advantage of that fact to cut the work time in half while
producing accurate (and importantly, identical!) results compared to the
previous data. i only needed to cache a state of some of the processed
data and apply the new values.

it's amazing what one comparison operation can save. :)

(as a side note: the original computation work took an average of 23
hours for 65,000 data points. each point took 400ms to complete. after
using the above method, it was cut down to about 37,000 data
compilations with the simpler math work done for every point. but
still, 11 hours was too long.)

--
-- Charles Banas
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
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...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions 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...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.