一个简单循环中有多少个基本操作?

发布于 2024-10-18 08:20:29 字数 226 浏览 9 评论 0原文

我有一堆代码来查找原始操作。问题是,网络上并没有关于这个主题的详细资源。在这个循环中:

for i:=0 to n do
  print test
end

我们到底有多少步?在我的第一个猜测中,我会说 n+1 ,考虑到 n 表示循环次数,1 表示打印。后来我想,也许是我不够精确。不是有每次循环都给i加1的操作吗? 就此而言,我们有 n+n+1=2n+1。这是正确的吗?

I have a bunch of code to find the primitive operations for. The thing is that there aren't really many detailed resources out on the web on the subject. In this loop:

for i:=0 to n do
  print test
end

How many steps do we really have? In my first guess I would say n+1 considering n for the times looping and 1 for the print. Then I thought that maybe I am not precise enough. Isn't there an operation even to add 1 to i in every loop?
In that matter we have n+n+1=2n+1. Is that correct?

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

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

发布评论

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

评论(2

活泼老夫 2024-10-25 08:20:29

通过将循环重新转换为 while ,可以将循环分解为“原始操作”:

int i = 0;
while (i < n)
{
    print test;
    i = i + 1;
}

或者,更明确地说:

loop:
    if (i < n) goto done
    print test
    i = i + 1
    goto loop
done:

您可以看到,对于每次迭代,都有一个比较、一个增量、和一个goto。这只是循环开销。您必须添加循环中完成的任何工作。如果 print 被视为“原始操作”,那么您将有:

  • n+1 次比较
  • n 次调用 print
  • n 次增量
  • n+1 goto指令(完成后从循环中分支出来的指令)

现在,如何将所有指令转换为机器代码高度依赖于编译器、运行时库、操作系统和目标硬件。也许还有其他事情。

The loop can be broken down into its "primitive operations" by re-casting it as a while:

int i = 0;
while (i < n)
{
    print test;
    i = i + 1;
}

Or, more explicitly:

loop:
    if (i < n) goto done
    print test
    i = i + 1
    goto loop
done:

You can then see that for each iteration, there is a comparison, an increment, and a goto. That's just the loop overhead. You'd have to add to that whatever work is done in the loop. If the print is considered a "primitive operation," then you have:

  • n+1 comparisons
  • n calls to print
  • n increments
  • n+1 goto instructions (one to branch out of the loop when done)

Now, how that all gets converted to machine code is highly dependent on the compiler, the runtime library, the operating system, and the target hardware. And perhaps other things.

少女七分熟 2024-10-25 08:20:29

这个问题可能是十年前的问题,但是由于该问题的核心主题与算法相关,即使在这样一段时间内也很重要,我觉得了解它很重要。
在中性算法语言中,让我们看一下下面的示例:

  1. sum ← 0
  2. for i ← 0 to n-1 do
  3.   sum ← sum + 1

因此,我们已经知道当基本运算由算法计算时恰好存在原始运算。
主要当:

  A.当执行算术运算时(例如+、-、*等)
  B.比较两个操作数时,
  C.给变量赋值时,
  D.当用值索引数组时,
  E.调用方法时,
  F.当从方法返回时,
  G.当遵循对象引用时。

因此,当用最终的原始操作证明上述场景的合理性时,我们发现:

  I.分配给 sum 时的一个基本操作。

二.简单 for 循环中的 n+1 次比较(请注意,您已从 0 到 n-1 比较了 n 次,另外 1 次比较未能检查 i < m。所以总共 n+1 次比较。

III.第三行 sum 有两个基本操作; 1 分配给 sum ,另一个 1 执行算术运算,因为它在简单的 for 循环中检查了 n 次,所以变成 2*n= 2n;

最后一个。伪代码阴影下的增量可以显式表示为 i ← i+1,它有两个原始操作,执行了 n 次 2*n= 2n;

总体而言,上面的示例总共有 1 + 1 + n + 2n; + 2n = 5n+2 个原始操作

This question may be from a decade ago however since the core theme of the question is of algorithm related that is important even over such period of time I feel it is critical knowing it.
In Neutral Algorithm language let's look into the below example:

  1. sum ← 0
  2. for i ← 0 to n-1 do
  3.   sum ← sum + 1

so, we already know primitive operations happens to exist when basic operations are computed by an algorithm.
Mainly when:

  A. When arithmetic operations are performed (e.g +, -, *, etc)
  B. When comparing two operands,
  C. When assigning a value to variable,
  D. When indexing an array with a value,
  E. When calling a method,
  F. When returning from a method and,
  G. When following object reference.

so, when justify the above scenario with ultimate primitive operations we find that:

  I. ONE basic operation when assigning to sum.

  II. n+1 comparisons in the simple for loop (mind you have compared n times from 0 to n-1 and the other 1 comparison is which failed checking i < m. so total n+1 comparisons.

  III. The third line sum has two primitive operations; 1 assigning to sum and another 1 performing arithmetic operations. which is since it is checked n times in the simple for loop it becomes 2*n= 2n;

  IV. Last one but that is shadowed by the pseudocode is the increment which can be explicitly represented as i ← i+1 which has two primitive operation that is gone n times 2*n= 2n;

Overall the above example has a total of 1 + 1 + n + 2n + 2n = 5n+2 primitive operations

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