Optimization, C++

Started by
13 comments, last by iMalc 17 years, 6 months ago
Thanks, Im getting the profiler and then I'll try to figure out how it works.
Reading all your posts I guess the only right way is to profile whatever alternative codepieces you have
and then make the conclusion on which one to use.

Thanks to Way Walker for clearing out the array-thing, but I think I got missunderstod from the begining.
My question is really this:

int Somevalue,
Someothervalue;

Is this piece of code more efficent:

for(Somevalue=inital_value; Somevalue < max; somevalue += something)
{
for(someothervalue=inital_value2; someothervalue < max2; someothervalue += somethingelse)
{
foo[Index] = Somevalue;
foo[Index + 1] = foo[Index] + someothervalue;
}
}

then this :

for(Somevalue=inital_value; Somevalue < max; somevalue += something)
{
for(someothervalue=inital_value2; someothervalue < max2; someothervalue += somethingelse)
{
foo[Index] = Somevalue;
foo[Index + 1] = Somevalue + somethingelse;
}
}

?
The code I have is not this simple and it would take quite some time to explain conceptually what it does, but I think
this conveys the issue, if not I'll try to clearify.
In other words, is it more efficent to assign from a dynamic array where a value is stored or is it more efficent to
calculate the same value that you have in the array when the value you are calculating does not require a lot of
math ops?
An other way to put it migth be (not sure if it is correct to put it like this) "what is the overhead for getting a value
from a dynamic array"?

I know the speed of the algorithm is sufficent for my solution, so now I also want to make the code as efficent as possible -
not because it's to slow but rather because I like the challenge of optimization and I want to learn how to write as efficent
code as possible.
Advertisement
The fastest, above, would be:

const int x = (max - initial_value) % something + initial_value;const int y = x + (max2 - initial_value2) % somethingelse + initial_value2;foo[Index] = x;foo[Index + 1] = y;


Since the loops are obviously not required.
Quote:Original post by Dive_With_Me
Thanks, Im getting the profiler and then I'll try to figure out how it works.
Reading all your posts I guess the only right way is to profile whatever alternative codepieces you have
and then make the conclusion on which one to use.

Thanks to Way Walker for clearing out the array-thing, but I think I got missunderstod from the begining.
My question is really this:

int Somevalue,
Someothervalue;

Is this piece of code more efficent:

for(Somevalue=inital_value; Somevalue < max; somevalue += something)
{
for(someothervalue=inital_value2; someothervalue < max2; someothervalue += somethingelse)
{
foo[Index] = Somevalue;
foo[Index + 1] = foo[Index] + someothervalue;
}
}

then this :

for(Somevalue=inital_value; Somevalue < max; somevalue += something)
{
for(someothervalue=inital_value2; someothervalue < max2; someothervalue += somethingelse)
{
foo[Index] = Somevalue;
foo[Index + 1] = Somevalue + somethingelse;
}
}

?
The code I have is not this simple and it would take quite some time to explain conceptually what it does, but I think
this conveys the issue, if not I'll try to clearify.
In other words, is it more efficent to assign from a dynamic array where a value is stored or is it more efficent to
calculate the same value that you have in the array when the value you are calculating does not require a lot of
math ops?
An other way to put it migth be (not sure if it is correct to put it like this) "what is the overhead for getting a value
from a dynamic array"?

I know the speed of the algorithm is sufficent for my solution, so now I also want to make the code as efficent as possible -
not because it's to slow but rather because I like the challenge of optimization and I want to learn how to write as efficent
code as possible.

In your example Somevalue is a value, not an expression, therefore it's not evaluted like you seem to believe. If we have this code:
int a = 5*100;int b = a;int c = a;

Then what happens (no optimizer, x86 like architecture) is that we put 5 in a register, multiply that register by 100. We then save the value of the register to some memory location. We then move the contents of the memory location to a new memory location (b), and the when do the same again but to a new memory location (c). So 5*100 is only evaluted once.

Accesing a specific element of an array is almost always (I can't imagine a system where this isn't true) slower than accesing a specific memory location.

Access single variable a:
1. Load contents at &a

Access element b of array a:
1. Load contents at &b
2. Multiply by size of one element of a (this is a compile time constant, and will be hardcoded in the assembly, therefore no need to load it)
3. Add with &a
4. Load the contents of the value we now have

If what you mean is whether
1) a = f()
b = f()
or
2) a = f()
b = a
is the fastest, then, that depends on how much work f needs to do. If f needs to do alot, number two is the fastest, but the optimizer should be able to figure this out itself, so do whatever is the most readable.
Thanks again for the input.

First of all:
Loops are necessary in my case.
When I wrote Index I ment it to be an integer that I would increase depending on some conditions in my loops - not a literal that does not change in the loop.
Sorry for that.

Yes, it depends on how much work f() needs to do, that is my question - when should I choose to calculate the value and when should I use one that is stored in a dynamic array.
My guess is that the profiler will have the best answer to this question so I'll run it by him =)
Quote:Original post by Dive_With_Me
What do you mean with that "an array access at a non-constant index always requires an addition"?
What he means is that this:
val = a[x];
is exactly the same as this:
val = *(a + x);
or for that matter, this:
val = *(x + a);
and strangely enough, even this:
val = x[a];
Quote:Yes, this is micro-optimization.
But micro-optimization can be good when you are dealing with a small piece of code that is looped continously in a program - i.e. this is small but critical code that needs to be fast.
Sure, but the only way to know which loops are actually taking a decent amount of time, and are worth optimising is by profiling.
Even better, by performing higher level optimisations first, you might be able to do away with the loop entirely, or something like that.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement