如何计算嵌套循环的时间成本 [英] How to calculate the time cost of a nested loop

查看:86
本文介绍了如何计算嵌套循环的时间成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Windows计算器中放入一个数学公式来计算嵌套循环的时间成本吗?

Is there a math formula that I can put in my Windows calculator to calculate the time cost of my nested loop?

从我的阅读中,我看到类似O(n),但我不知道如何将其放入计算器。

From what I read, I saw things like O(n) but I don't know how to put that in a calculator.

我有一个300级嵌套循环,每个循环级将从0变为1000

I have a 300 levels nested loop, each loop level will go from 0 to 1000

所以它就像:

for i as integer =0 to 1000
{
 //300 more nested loops, each goes from 0 to 1000
}

我假设1个完整的循环将花费1秒(即,从最顶部的循环到最内部的循环的300个水平将花费1秒)。

I am assuming that 1 complete loop will take 1 second (i.e. the 300 levels from the top most loop to the inner most loop will take 1 second).

我如何找出完成整个循环操作需要多少秒?

How can I find out how many seconds it will take to complete the whole loop operation?

谢谢

推荐答案

如果您有每个 N 个步骤的 M 嵌套循环,则为最内层代码的总次数将运行 N M 。因此,如果执行此代码需要 t 秒,则总运行时间将不少于 N M t

If you have M nested loops each with N steps, the total number of times the inner-most code will run is NM. Consequently, if this code takes t seconds to execute, the total running time will be no less than NMt.

在您的情况下, N   =  = 1001和 M   =  301。因此,

In your case, N = 1001 and M = 301. Thus

N M   =  = 。

如果在广告中因此,我们假设最里面的代码确实非常快,例如 t   =  1  µs,总运行时间将不少于 N < sup> M t   =  1.35 ... ×  10 897   s。

If, in addition, we assume that the inner-most code is really fast, like t = 1 µs, the total running time will be no less than NMt = 1.35... × 10897 s.

当宇宙结束时,您的代码将几乎完成0.0000000000000000000000000000 %的工作,前提是当时硬件尚未出现故障,人类文明还没有完成。到头来(这使您很难找到运行硬件所需的电源)。

When the universe comes to its end, your code will be at almost exactly 0.0000000000000000000000000000 % completion, assuming the hardware hasn't faulted by then and the human civilization hasn't come to an end (thus making it difficult for you to find the electrical power you need to run your hardware).

这篇关于如何计算嵌套循环的时间成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆