Pages

Wednesday, June 10, 2009

Tight Loops

I got this term within last couple of days. It was very interesting to me. Oh ho I think I should clarify I am talking about the loops in programming. Tight loops are born to exhaust the CPU power like this.

 

           

            For(int i = 0; i< 1000 ; i++)

                        {

                                    //do something but don't sleep

}

 

 

When this loop runs no other code will get the chance to be executed. So this is a tight loop. We can make it loose in this way.

 

 

For(int i = 0; i< 1000 ; i++)

                        {

                                    //do something

                                    Thread.sleep(100); // sleep for 100 ms

}

 

 

Tight loops are good when you are calculating something. But when you need to communicate with other elements that may be network, file or may be another thread, loose loops are fine. In my previous post I talked about a network connecting problem. Loose loops solved that for me.

 

Thanks to loose loops working tightly with each other.




--
http://ifteebuet.blogspot.com

No comments:

Post a Comment