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

前端 未结 8 753
鱼传尺愫
鱼传尺愫 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:25

    I can suppose that in first variant you find strlen each iteration, while in second variant you don't do that.
    Try this to check:

    int a = strlen(x); 
    for (int i = 0; i < a; i++) {...}
    

提交回复
热议问题