static variables question

J

Jan Bernatik

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.
 
J

John Harrison

Jan Bernatik said:
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
 
K

Karl Heinz Buchegger

Jan said:
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.
 
B

Benoit Mathieu

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
 
S

Siemel Naran

Jan Bernatik said:
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.
 
R

Raju Dantuluri

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);
}
 
J

Jan Bernatik

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.
 
C

Charles Banas

Karl said:
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.)
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top