1

I have following code inside a loop:

ekp = e[k][p];
hkp = h[k][p];
uk = round(ekp);
u[k] = uk;
yk = (ekp - uk) / hkp;
y[p] = yk;

The variables are declared the following way:

float ekp, yk, hkp;
int uk;
float **e, *y, **h;
int *u;

I use local variables to store the values from arrays to access them less times. When I profile the code with Xcode I get 9.3% of total execution time on

y[p] = yk;

and only 2.7% on

u[k] = uk;

Why is there such a great difference between storing an int to an array and storing a float?

Would using declaring the variables the following way be more efficient?

register float ekp, yk, hkp;
register int uk;
fc67
  • 409
  • 5
  • 17
  • try changing the loop iteration order. eg, from first row and then column to the inverse, report back – Paulo Neves Apr 11 '14 at 11:39
  • Consider giving your variables intuitive, self-explaining names. – Lundin Apr 11 '14 at 11:42
  • 1
    @Lundin I don't know what kind of task is it. But in the field of keen computations its often preferable to name variables just like in algorithm description (in referenced book, article, TeX docs etc), which often like 'yk', 'T', 'delta' and so on. – Yury Apr 11 '14 at 12:17
  • Exactly what Yuri said. This is an implementation based on a paper and I kept the names from there. @aiwarrior I am already doing that. The matrices are traversed from right to left and top to down. The y array is traversed from left to right while the u array is traversed from right to left. But even when I had the code traversing in the same direction I had the same problem. – fc67 Apr 11 '14 at 13:13
  • But in that case, you provide comments that explain what the cryptic letters actually mean. Overall, mathematics has got _a lot_ to learn from programming when it comes to readability: if the math formula was written by some weirdo obfuscation-lover, it doesn't necessarily mean that you should obfuscate your program to the same poor level. – Lundin Apr 11 '14 at 13:21
  • Posting the code as profiled, including the types of `p` and `k` would make for an easier analysis. – chux - Reinstate Monica Apr 11 '14 at 14:50
  • p and k are int's and only used as index of the arrays. The only operations with them are increments and decrements. – fc67 Apr 11 '14 at 18:46

3 Answers3

2

First of all, it is usually pointless to discuss a particular program's performance without any specific system and hardware in mind.

On a system where both int and float have the same size, there's no reason why there would be a performance difference. The division is what would take the most time in this program, and since the supposedly slow operation happened just after the division, I suspect that you shouldn't trust the benchmarking results all that well.

What happens if you change the code to

yk = (ekp - uk) / hkp;
u[k] = uk;
y[p] = yk;

There should be no difference, so if you experience one, the tool is not to be trusted. It might be that the yk variable gets optimized away, so that the source code lines don't correspond 1:1 to the machine code.

Would using declaring the variables the following way be more efficient?

No, register is an obsolete keyword from the dark ages, when compilers were barely able to optimize anything. A modern compiler doesn't need it, it will make much better optimizing decisions than the programmer.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I am testing it on an Intel I5-2415M @2.3GHz with 4GB of RAM. When I do that change the times keep the same. – fc67 Apr 11 '14 at 18:43
0

register key word can be used with float. if the float doesn't fit in any of the registers then the compiler just ignores it. register keyword is only a suggestion to the compiler it is not mandatory.

As for the other question i am not sure. i looked at assembly code. May be you can try http://assembly.ynh.io/ to see the assembly code.

abhilb
  • 5,639
  • 2
  • 20
  • 26
0

This is telling you about 12% of the time.

What is the other 88% doing?

If you try this method you will find out.

Don't make the drunk's mistake of searching for keys under the street lamp because that's where the light is.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135