dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: All
May 19, 1999

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date