计算Python中给定功能的运行时间

发布于 2025-01-20 02:29:47 字数 1389 浏览 0 评论 0原文

我对运行时有点陌生,所以我正在尝试基本的例子。

以下面的函数为例,我将展示我计算其运行时间的尝试。下面的函数引用 BinarySearchTree,其中“ue”代表“u”的左子节点,“ud”代表“u”的右子节点。另外,“up”代表“u”的父级,“self.r”代表二叉搜索树的根。我相信这对我的问题并不重要,但这只是为了提供一些背景信息。

     function                           |       cost      |       times (run)
                                        |                 | 
def splice(self,u):                     |                 |
    if u.e is not None: w = u.e         |        c1       |           1          
    else: w = u.d                       |        c2       |           1
    if u == self.r:                     |                 |
        self.r = w                      |        c3       |           1
        p = None                        |        c4       |           1
    else:                               |                 |
        p = u.p                         |        c5       |           1       
        if p.e == u: p.e = w            |        c6       |           1 
        else: p.d = w                   |        c7       |           1 
    if w is not None: w.p = p           |        c8       |           1

在这个例子中,我相信考虑最坏的情况并不重要(因为运行时间将相等(或几乎相等))。不过,让我们想象一下:

最坏的情况是当我们输入第二个 else 语句并且同时输入最后一个 if 语句时。第一个 if-else 语句基本上是相等的,因此输入哪一个并不重要。因此,最坏情况下的运行时间将通过以下方式获得:

T(n) = c1 + c5 + c6 + c8 = c,对于某个常数 c

这是否正确?这是否意味着该算法的时间复杂度是 O(1) ?即,恒定时间?

I am kind of new to running time and so I am trying to basic examples.

Take the following function as example and I will show my attempt to compute its running time. The function below refers to a BinarySearchTree where 'u.e.' represents the left child of 'u' and 'u.d' represents the right child of 'u'. Additionaly, 'u.p' represents the parent of 'u' and 'self.r' represents the root of our binary search tree. I believe this isn't important to my question but it's just to give some context.

     function                           |       cost      |       times (run)
                                        |                 | 
def splice(self,u):                     |                 |
    if u.e is not None: w = u.e         |        c1       |           1          
    else: w = u.d                       |        c2       |           1
    if u == self.r:                     |                 |
        self.r = w                      |        c3       |           1
        p = None                        |        c4       |           1
    else:                               |                 |
        p = u.p                         |        c5       |           1       
        if p.e == u: p.e = w            |        c6       |           1 
        else: p.d = w                   |        c7       |           1 
    if w is not None: w.p = p           |        c8       |           1

In this example, I believe it won't matter considering the worst case scenario (since the run time will be equal (or almost equal)). Altought, let's imagine it:

The worst case will be when we enter the second else statement and when we also enter the last if statement. The first if-else statements are basically equal and so it doesn't matter which one we enter. So, the worst case running time will be obtained by:

T(n) = c1 + c5 + c6 + c8 = c, for some constant c

Is this correct? And does this mean that the time complexity of this algorithm is O(1) ? I.e., constant time?

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

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

发布评论

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

评论(2

明月夜 2025-01-27 02:29:47

没错,这是O(1),但是当我们分析时间复杂性时,我们宁愿专注于每一行,因为每个基本操作都是恒定的。我们询问更多有关执行THS线的次数:

def foo(n):
    for i in range(n):
        if 1:
            ...
        if 2:
            ...
        if 3:
            ...
        # no matter how many ifs are there

我们运行循环n次,因此复杂性将是(如果的身体具有基本操作)o(n)o(n)。如果您有两个嵌套环,那将是o(n*m),其中 n,m 是该循环的迭代次数。不幸的是,它并不总是那么简单,并且变得更加棘手,例如递归功能。我建议您阅读本文以获取更多洞察力。

That is right, it's O(1), however when we analyse time complexity we rather focus on every line, as every single basic operation is constant. We ask more about how many times ths lines are executed:

def foo(n):
    for i in range(n):
        if 1:
            ...
        if 2:
            ...
        if 3:
            ...
        # no matter how many ifs are there

We run loop n times, so complexity would be (assuming ifs body has basic operations) O(n). If you have two nested loops, it would be O(n*m) where n, m are number of iterations of this loops. Unfortunately it's not always so simple and becomes more tricky e.g. in recursive function. I recommend you to read this article to get more insight into O notation.

故乡的云 2025-01-27 02:29:47

是的。通常,如果您没有在功能中发生的无限大小的循环,则运行时间将为O(1)。

如果代码的某些部分多次执行,则函数将超过O(1),具体取决于输入的大小“ N”。

Yes. In general, if you have no loops of indefinite size occurring in a function, the run time will be O(1).

A function will exceed O(1) if some parts of your code are executing multiple times depending on the size of your input, 'n'.

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