88

I've heard that phrase a lot. What does it mean?

An example would help.

skaffman
  • 398,947
  • 96
  • 818
  • 769
G S
  • 35,511
  • 22
  • 84
  • 118

8 Answers8

57

From Wiktionary:

  1. (computing) In assembly languages, a loop which contains few instructions and iterates many times.
  2. (computing) Such a loop which heavily uses I/O or processing resources, failing to adequately share them with other programs running in the operating system.

For case 1 it is probably like

for (unsigned int i = 0; i < 0xffffffff; ++ i) {}
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 7
    1) The example is not a tight loop, it's a NOP :) 2) I've never seen this meaning in my life. It sounds more like a "resource hog" – Karoly Horvath Nov 17 '15 at 06:31
  • 2
    a smart(ish) compiler with some smart(ish) optimizations will get rid of it. – djh Mar 11 '16 at 19:16
  • 1
    Continuing from the above comments, wouldn't `while (condition == false) {}` be a better example? – jdrd Nov 20 '18 at 17:48
  • I think this loop, and even the one @jdrd posted will just use all the performance of one CPU core. Why do people use it? – Minh Nghĩa May 03 '20 at 13:56
32

I think the phrase is generally used to designate a loop which iterates many times, and which can have a serious effect on the program's performance - that is, it can use a lot of CPU cycles. Usually you would hear this phrase in a discussion of optimization.

For examples, I think of gaming, where a loop might need to process every pixel on the screen, or scientific app, where a loop is processing entries in giant arrays of data points.

Ray
  • 21,485
  • 5
  • 48
  • 64
  • 4
    A portion of the code that gets executed often may be a good candidate for optimisation, but it is not a tight loop in general. Even if it gets called often - if the calling happens through several layers of functions and objects, it's not "tight". The loop condition and the loop instructions need to be really tight to each other, code in the same file and same few lines for a "tight loop". – foo Feb 19 '11 at 02:08
  • I've heard the sort of loop described in the answer called a "hot spot" in a program -- a place where targeted optimization will pay off. For this very reason, Sun named their adaptive profile-driven just-in-time compiling technology "HotSpot". – Jeffrey Hantin Feb 28 '12 at 10:37
  • 1
    @foo: They don't have to be in the same file at all. Modern compilers can hapilly eliminate layers of abstractions into nothing... (e.g.: C++ templates) – Karoly Horvath Nov 17 '15 at 06:33
18

A tight loop is one which is CPU cache-friendly. It is a loop which fits in the instruction cache, which does no branching, and which effectively hides memory fetch latency for data being processed.

SandeepJ
  • 341
  • 4
  • 10
14

There's a good example of a tight loop (~ infinite loop) in the video Jon Skeet and Tony the Pony.

The example is:

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

which produces a tight loop because IndexOf ignores a Unicode zero-width character (thus finds two adjacent spaces) but Replace does not ignore them (thus not replacing any adjacent spaces).

There are already good definitions in the other answers, so I don't mention them again.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
AndiDog
  • 68,631
  • 21
  • 159
  • 205
5

SandeepJ's answer is the correct one in the context of Network appliances (for an example, see Wikipedia entry on middlebox) that deal with packets. I would like to add that the thread/task running the tight loop tries to remain scheduled on a single CPU and not get context switched out.

Deepak Mohanty
  • 121
  • 1
  • 2
4

According to Webster's dictionary, "A loop of code that executes without releasing any resources to other programs or the operating system."

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

Pawel J. Wal
  • 1,166
  • 1
  • 11
  • 24
  • I've seen that, but could you illustrate with an example? – G S Feb 06 '10 at 11:54
  • 3
    Actually, I disagree strongly with this definition. as most loops don't release such resources, it implies that most loops are tight loops. –  Feb 06 '10 at 11:55
4

From experience, I've noticed that if ever you're trying to do a loop that runs indefinitely, for instance something like:

while(true)
{
    //do some processing
}

Such a loop will most likely always be resource intensive. If you check the CPU and Memory usage by the process with this loop, you will find that it will have shot up. Such is the idea some people call a "tight loop".

ankapaul
  • 49
  • 1
0

Many programming environments nowadays don't expose the programmer to the conditions that need a tight-loop e.g. Java web-services run in a container that calls your code and you need to minimise/eliminate loops within a servlet implementation. Systems like Node.js handle the tight-loop and again you should minimise/eliminate loops in your own code. They're used in cases where you have complete control of program execution e.g. OS or real-time/embedded environments. In the case of an OS you can think of the CPU being in an idle state as the amount of time it spends in the tight-loop because the tight-loop is where it performs checks to see if other processes need to run or if there are queues that need serviced so if there are no processes that need to run and queues are empty then the CPU is just whizzing round the tight-loop and this produces a notional indication of how 'not busy' the CPU is. A tight-loop should be designed as just performing checks so it just becomes like a big list of if .. then statements so in Assembly it will boil down to a COMPARE operand and then a branch so it is very efficient. When all the checks result in not branching, the tight-loop can execute millions of times per second. Operating/embedded Systems usually have some detection for CPU hogging to handle cases where some process has not relinquished control of the CPU - checks for this type of occurrence could be performed in the tight-loop. Ultimately you need to understand that a program needs to have a loop at some point otherwise you couldn't do anything useful with a CPU so if you never see the need for a loop it's because your environment handles all that for you. A CPU keeps executing instructions until there's nothing left to execute or you get a crash so a program like an OS must have a tight-loop to actually function otherwise it would run for just microseconds.

Jim R
  • 1