advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 3/5 | Rate this item | 1 user has rated this item.
Expertise: All
Language: C++
May 19, 1999
Unroll Loops to Optimize Your Code
A compiler can automatically optimize the code by unrolling loops. Consider this code:
 
  int *buff = new int[3];
  for (int i =0; i<3; i++)
 { 
   buff[i] = 0;
 }
On every iteration, the loop assigns a value to the next array element. However, precious CPU time is also wasted on testing and incrementing the loop counter, and performing a jump statement. To avoid this overhead, the compiler can unroll the loop into a sequence of three assignment statements:
 
  buff[0] = 0; 
  buff[1] = 0; 
  buff[2] = 0; 
This way, you avoid the unnecessary overhead of a loop. Note, however, that the compiler applies this optimization automatically; you shouldn't do it yourself.
Danny Kalev
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About