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

前端 未结 8 747
鱼传尺愫
鱼传尺愫 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++) {...}
    
    0 讨论(0)
  • 2020-12-07 01:28

    If no there's no compilation optimization. Yes because strlen will iterate on every byte of the string every time and the second implementation will do it only once.

    0 讨论(0)
提交回复
热议问题