What is a tight loop?

I have heard this phrase a lot. What does it mean?

An example will help.

+56
terminology
Feb 06 '10 at 11:50
source share
7 answers

From Wiktionary :

  • (calculation) In assembly languages, a loop that contains several instructions and is repeated many times.
  • (calculation) Such a loop, which makes heavy use of I / O or processing resources, does not allow for adequate exchange with other programs running on the operating system.

For case 1, this probably looks like

for (unsigned int i = 0; i < 0xffffffff; ++ i) {} 
+42
Feb 06 '10 at
source share

I think that this phrase is usually used to refer to a cycle that is repeated many times and which can seriously affect program performance, that is, it can use many processor cycles. You will usually hear this phrase in a discussion of optimization.

As an example, I think of games where the loop may need to process every pixel on the screen or a scientific application where the loop processes records in gigantic arrays of data points.

+24
06 Feb '10 at 12:10
source share

A good example of a tight loop (endless loop) in the video is John Skeet and Tony Pony .

Example:

 while(text.IndexOf(" ") != -1) text = text.Replace(" ", " "); 

which creates a closed loop because IndexOf ignores the Unicode zero-width character (thus finding two adjacent spaces), but Replace does not ignore them (thus not replacing adjacent spaces).

Other answers already have good definitions, so I don't mention them again.

+8
Feb 06 '10 at 12:10
source share

A hard loop container is one that doesn't look like a processor. This is a loop that fits into the instruction cache, which does not have a branch and which effectively hides the data retention delay for the data being processed.

+4
Nov 14 '14 at 6:50
source share

SandeepJ's answer is correct in the context of network devices (for example, see the Wikipedia entry on the intermediate server) that deal with packets. I would like to add that a thread / task executing a narrow loop is trying to stay scheduled on one CPU and not disconnect the context.

+3
Dec 05 '14 at 19:22
source share

According to Webster’s dictionary, “a loop of code that runs without releasing any resources for other programs or the operating system.”

http://www.websters-online-dictionary.org/ti/tight+loop.html

+2
Feb 06 '10 at 11:52
source share

From experience, I noticed that if you try to make a loop that runs indefinitely, for example, something like:

 while(true) { //do some processing } 

Such a loop is likely to always be resource intensive. If you check the CPU and memory usage in this process with this loop, you will find that it will take off. This is an idea that some people call the “narrow loop”.

+1
Dec 21 '15 at 10:38
source share



All Articles