What is a “tight loop”?

前端 未结 8 723
说谎
说谎 2020-12-01 06:56

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

An example would help.

8条回答
  •  广开言路
    2020-12-01 07:47

    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.

提交回复
热议问题