【问题标题】:How can I automate calculation of time complexity of algorithms in JavaScript? [duplicate]如何自动计算 JavaScript 中算法的时间复杂度? [复制]
【发布时间】:2020-05-01 19:01:38
【问题描述】:

JavaScript 中是否有计算算法时间复杂度的 inbuild 方法?

例如,如果我输入这个函数:

function solution(A) {
    let smallestInt = 1;

    function existsInArray(val) {
        return A.find((a) => a === val);
    }

    for (let index = smallestInt; index < 1000000; index++) {
        if (existsInArray(index) && !existsInArray(index + 1) &&
            existsInArray(smallestInt)) {
            smallestInt = index + 1
        }
    }
    return smallestInt;
}

该方法应该能够告诉我该算法的时间复杂度是 O(N*2) 或者哪个正确答案会是?

如果没有可用的 inbuild 方法,是否有带有自动化方法的库来实现这一点?

【问题讨论】:

    标签: javascript ecmascript-6 time-complexity big-o


    【解决方案1】:

    没有。这样的程序将解决halting problem。这是不可能的。

    【讨论】:

    • 您发布的链接无效
    • @eugen 谢谢,已修复(hvala brate)
    【解决方案2】:

    通常不会,尽管可以对不同的输入进行多次模拟以得出合理的近似值。

    查看以下三个有用的讨论:

    1. Are runtime bounds in P decidable? (answer: no)
    2. Programmatically obtaining Big-O efficiency of code
    3. A tool for calculating the big-O time complexity of Java code?

    【讨论】:

    • 那里有一些有用的链接。谢谢。跟进这些资源提供了一个总体要点,即没有图书馆(还)可以实现我正在寻找的东西。但是搜索并不完整。我想知道codility 等平台如何为平台上编写的算法执行自动时间复杂度。
    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多