• magic_lobster_party@kbin.run
    link
    fedilink
    arrow-up
    5
    ·
    4 months ago

    Time complexity is mostly useful in theoretical computer science. In practice it’s rare you need to accurately estimate time complexity. If it’s fast, then it’s fast. If it’s slow, then you should try to make it faster. Often it’s not about optimizing the time complexity to make the code faster.

    All you really need to know is:

    • Array lookup: O(1)
    • Single for loop: O(n)
    • Double nested for loop: O(n^2)
    • Triple nested for loop: O(n^3)
    • Sorting: O(n log n)

    There are exceptions, so don’t always follow these rules blindly.

    It’s hard to just “accidentally” write code that’s O(n!), so don’t worry about it too much.