Aside from using two separate loops, the original code contains lots of redundant statements. One possible improvement would be this:
int tmp = 6 * h;
for (j = 1; j <= 4; j++) {
a[j] = tmp;
b[j] = 6 + 3 * j * j;
}
The main optimizations:
- One loop instead on two
- Removed assignments that get overwritten in the next statement
- Calculate a value that does not depend on the loop index outside the loop
You should be aware that most of these optimizations would be done by a compiler anyways.
4
solved How do I optimize C code using loop transformation? [closed]