Loop optomization Q

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I currently have this code:

int l_ob_cold = object-cold;
int l_ob_set = object-setpoint;
int l_hot_amb = hot-ambient;
for (int i = 0; i < array_size; ++i)
	{
		ob_cold[i] = l_ob_cold;
		ob_set[i] = l_ob_set;
		ob_amb[i] = l_ob_amb;
	};

Should I split this up into 3 seperate loops, one for each array? Should I initialize each l_ variable inside the for header? Etc, in other words what should I do for it to be most optomized?

Edward

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The general rule with optimizers is to "expose" as much as possible to the optimizer. So, bigger blocks of straight line code are better. More opportunities for constant folding are better. Make it easier for the compiler to identify loop-invariants, induction variables, and opportunities for strength reduction. Eschew branchiness.

In the case of the code you present, the l_* variables should disappear with optimization, they are redundant copies. If, as you suggested, the code were rewritten as three congruent loops, a smart compiler will try to do to "loop jamming" and create a single loop in order to get a bigger chunk of straight line code as optimization fodder (see above). In a compiler that does loop jamming, the two forms of the loop produce identical code. (I don't know if gcc does loop jamming.)

In general, people worry too much about writing for the optimizer, and worry about the wrong things. If you simply write clean code, the optimizer will do a good job of analysis. To quote an old friend: "If it is easy for you to understand, it will be easy for the optimizer to understand." This old friend has been working on nothing but the most throttle-to-the-firewall optimizers since the days of the CDC7600, you can trust his opinion.

Now, here is how you actually learn something: stop reading now and go look at the disassembly. Try different optimization switches, try different code formulations. Look at what happens. When you get good enough at that to identify things the compiler could do better, send me your resume and I will forward it to the right people. :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Should I initialize each l_ variable inside the for header?

This wouldn't make any difference at all.

Quote:
Should I split this up into 3 seperate loops, one for each array?

Since the AVR has three pointer registers and you have three arrays, it would take more time and space if you split it into three separate loops. If you had more than three it might be advantageous to split it, but do three arrays in each loop, except for the last which would have the remaining array or two. Even then it might be better to keep it in one loop. You just have to try it both ways and see what happens.

Quote:
in other words what should I do for it to be most optomized?

This depends on how you want it optimized. For speed? For size? What tricks you might apply will depend on what you want out of it.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@dbc: Yup I think I am getting ahead of myself with questions about optimization. I should finnish the damn code first so it does what I want and then go back through and clean/optimize things up.

@Koshchi: I'll be optomizing this for speed, I plan on using an atmega644 so program space shouldn't be an issue.

Thanks guys,
Edward