Is using strlen() in the loop condition slower than just checking for the null character?

前端 未结 8 815
鱼传尺愫
鱼传尺愫 2020-12-07 00:39

I have read that use of strlen is more expensive than such testing like this:

We have a string x 100 characters long.

I think that<

8条回答
  •  情书的邮戳
    2020-12-07 01:05

    The first code checks length of x in every iteration of i and it takes O(n) to find the last 0, so it takes O(n^2), the second case is O(n)

提交回复
热议问题