什么是“紧循环”?

发布于 2024-08-21 00:35:47 字数 43 浏览 3 评论 0原文

这句话我听过很多次了。这是什么意思?

举个例子会有帮助。

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

An example would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

空城缀染半城烟沙 2024-08-28 00:35:47

来自 维基词典:(

  1. 计算)在汇编语言中,包含少量指令并迭代多次的循环。
  2. (计算)此类循环大量使用 I/O 或处理资源,无法与操作系统中运行的其他程序充分共享它们。

对于情况 1 可能是这样的

for (unsigned int i = 0; i < 0xffffffff; ++ i) {}

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) {}
眉黛浅 2024-08-28 00:35:47

我认为这个短语通常用来指定一个循环,它会迭代很多次,并且会对程序的性能产生严重影响——也就是说,它会使用大量的CPU周期。通常您会在优化讨论中听到这个短语。

例如,我想到游戏,其中循环可能需要处理屏幕上的每个像素,或者科学应用程序,其中循环处理巨大数据点数组中的条目。

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.

萌辣 2024-08-28 00:35:47

紧密循环是一种对 CPU 缓存友好的循环。它是一个适合指令高速缓存的循环,不进行分支,并且有效地隐藏了正在处理的数据的内存获取延迟。

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.

记忆里有你的影子 2024-08-28 00:35:47

视频 Jon Skeet 和小马托尼

示例是:

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

这会产生一个紧密循环,因为 IndexOf 忽略 Unicode 零宽度字符(从而找到两个相邻的空格),但 Replace 不会忽略它们(因此不会替换任何相邻的空间)。

其他答案已经有很好的定义了,我就不再提了。

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.

沫雨熙 2024-08-28 00:35:47

根据韦氏词典,“执行时不向其他程序或操作系统释放任何资源的代码循环”。

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

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

眼眸里的那抹悲凉 2024-08-28 00:35:47

SandeepJ 的答案是处理数据包的网络设备(例如,请参阅中间盒上的维基百科条目)上下文中的正确答案。我想补充一点,运行紧密循环的线程/任务会尝试在单个 CPU 上保持调度状态,并且不会切换上下文。

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.

月亮坠入山谷 2024-08-28 00:35:47

根据经验,我注意到,如果您尝试执行无限期运行的循环,例如:

while(true)
{
    //do some processing
}

这样的循环很可能始终是资源密集型的。如果你用这个循环检查进程的CPU和内存使用情况,你会发现它会猛增。这就是一些人称之为“紧循环”的想法。

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".

小清晰的声音 2024-08-28 00:35:47

如今,许多编程环境不会让程序员面临需要紧密循环的条件,例如,Java Web 服务在调用您的代码的容器中运行,而您需要最小化/消除 servlet 实现中的循环。像 Node.js 这样的系统可以处理紧密循环,因此您应该再次最小化/消除自己代码中的循环。它们用于您可以完全控制程序执行的情况,例如操作系统或实时/嵌入式环境。对于操作系统,您可以将 CPU 处于空闲状态视为它在紧密循环中花费的时间,因为紧密循环是它执行检查以查看其他进程是否需要运行或是否有其他进程的地方。是需要服务的队列,因此,如果没有需要运行的进程并且队列为空,那么 CPU 只是在紧密循环中快速运转,这会产生 CPU 有多“不忙”的概念指示。紧密循环应设计为仅执行检查,因此它就像一个 if .. then 语句的大列表,因此在汇编中它将归结为 COMPARE 操作数,然后是一个分支,因此非常高效。当所有检查结果都没有分支时,紧密循环每秒可以执行数百万次。操作系统/嵌入式系统通常对 CPU 占用进行一些检测,以处理某些进程尚未放弃对 CPU 控制的情况 - 可以在紧密循环中执行对此类事件的检查。最终,您需要了解程序在某些时候需要有一个循环,否则您将无法使用 CPU 做任何有用的事情,因此,如果您从未发现需要循环,那是因为您的环境会为您处理所有这些事情。 CPU 会一直执行指令,直到没有任何东西可以执行或发生崩溃,因此像操作系统这样的程序必须有一个紧密循环才能实际运行,否则它只会运行几微秒。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文