complexity of code

前端 未结 6 1227
借酒劲吻你
借酒劲吻你 2021-01-14 07:33

what is the complexity of a program having only one loop, is it log n? can someone give me some ideas about estimating complexity of codes?

6条回答
  •  [愿得一人]
    2021-01-14 08:00

    If there's just one loop, it's probably the number of times that loop's body executes.... But of course you may have many hidden loops in library calls. It's easy to make a loop that executes n times O(n^2) or worse if you have strlen, memcpy, etc. taking place inside the loop body.

    Or even worse, if you have a library (or language) with regular expressions and their regex implementation is naive (like Perl), it's easy to make a program with just a single loop O(2^n)!! This cannot happen with a correctly written regex implementation.

提交回复
热议问题